我有一个网站使用Wordpress的自定义内容短代码插件。它有一些不兼容性,并使我们的网站几次失效。我正在做以下事情:
[loop type="th_sermons" taxonomy="th_sermons_speaker" value="speaker-slug" count="5"]
<li>
<a href="[field url]">
<span class="placeholder">[field thumbnail]</span>
<p>
<h5 class="rlcf-custom-sermon-list-title">[field title]</h5>
<span class="rlcf-custom-sermon-list-date">[field date]</span>
</p>
</a>
</li>
[if empty]
There are no sermons for this category.
[else]
<li>
<a href="/speakers/speaker-slug/">More Messages By Speaker Name</a>
</li>
[/if]
[/loop]
它工作时效果很好,但是将网站关闭并不是一个好结果。我认为编写一个可以实现此目的的自定义短代码会很容易。这样的事情。
[rlcf_sermons speaker="speaker-slug" count="optional_or_number"]
有人能指出我正确的方向吗?
谢谢!
答案 0 :(得分:0)
这应该指出您在主题functions.php
文件中添加自定义短代码的正确方向:
add_shortcode( 'rlcf_sermons', function( $atts ){
// set default attributes
extract( shortcode_atts( array(
'speaker' => '',
'count' => -1
), $atts ) );
// query
$sermons = new WP_Query(array(
'post_type' => 'th_sermons',
'tax_query' => array(
array(
'taxonomy' => 'th_sermons_speaker',
'field' => 'slug',
'terms' => $speaker
)
),
'posts_per_page' => $count
));
// output
if($sermons->have_posts()):
while ( $sermons->have_posts() ) :
$sermons->the_post();
// here goes all the code you want for each LI tag
echo '<li>' . get_the_title() . '</li>';
endwhile;
wp_reset_postdata();
else: echo 'There are no sermons for this category.';
endif;
} );