我正试图在主题中为相关帖子做一个选项我想显示两个不同的相关帖子,一个按标签,另一个按类别,但我想提供从主题面板中选择的可能性
我在relatedposts.php 中的代码
$relatedposts = get_option('theme_relatedoptions');
if (get_option('theme_relatedoptions') <> '') {
get_template_part( 'templates/related/'.$relatedposts.'', get_post_format() );
} else {
get_template_part( 'templates/related/tags', get_post_format() );
}
管理功能中的代码
$options[] = array( "name" => __('Related Videos','framework'),
"id" => $shortname."_relatedoptions",
"type" => "radio",
"options" => array("tags", "category"),
"std" => "tags");
相关文件夹中的tags.php按标签显示相关帖子category.php按类别显示相关帖子,但我不知道在哪里,我不确定get_template_part('templates/related/'.$relatedposts.'',get_post_format());
有人能帮我吗?感谢
实际上不能将$variable
直接传递给用get_template_part();
调用的模板,因为文件是required
:http://codex.wordpress.org/Function_Reference/get_template_part
要直接在get_template_part();
中使用变量,需要将该变量声明为全局变量,或者将自定义$arg
传递给WP_Query
。
话虽如此,我不明白为什么需要将变量传递给get_template_part();直接在这里。你能简单地读取变量(在这种情况下是类别或标记),然后将其作为一个类似于以下的参数传递给WP_Query吗:
$query = new WP_Query( 'tag='$variable );