具体地说,我正试图有条件地将一个推荐轮换器挂接到特定作者档案的侧栏中,并为每个特定作者提供单个帖子。
尽管我使用的是下面的一个特定示例,但我需要的一般答案是如何有条件地检查我是否在作者存档或该作者的单个帖子上,并设置一个唯一字符串以用于代码的另一部分。
在非代码语言中,我的目标是:
如果在author-1存档中,或者在author-1的一篇文章中,$var=x。
如果在author-2存档中,或在author-2的单个帖子中,$var=y。
如果您能指出我的代码不起作用的原因,我们将不胜感激。
下面我的函数的结果并不像预期的那样,它将最后一位作者的数据(在本例中为author-2)返回到所有作者档案和帖子侧边栏中,无论是哪个作者档案或哪个帖子作者。
以下是我的工作内容:
add_action (\'genesis_before_sidebar_widget_area\',\'user_testimonials\',10); // Genesis action hook.
function user_testimonials(){
global $post;
$author_id = $post->post_author; //Get the ID of the current author
$authornicename = get_the_author_meta( \'user_nicename\', $author_id ); //get the author nicename by ID
if (is_author() || is_singular(\'post\')){ // First make sure we are on an author archive or a single post.
if (is_author(\'author-1\') || $authornicename =\'author-1\' ){$rotatorid=445;}
//Here I have manually looked up and inserted the author nice-name into is_author()
//The expected result would be to return true if we are on that author\'s archive page
//Next I checked the value of $authornicename against a manually inserted and known nice-name.
//This should return true when on a single post by that author.
//The last part manually sets the $rotatorid to the known ID of the rotator that I want to display to be used to dynamically generate the output.
if (is_author(\'author-2\') || $authornicename =\'author-2\' ){$rotatorid=451;}
echo "<section class=\'widget\'><h3 class=\'widget-title\'> Testimonials </h3>";
echo do_shortcode(\'[testimonial_rotator id=\'. $rotatorid .\' hide_title="true" format="list"]\') . \'</section>\';
//A shortcode is used to output the testimonial rotator.
//$rotatorid is inserted to dynamically output the rotator for that author.
}
}