我无法在互联网上找到解决方案,也无法提前定制网站文档。我希望wordpress天才能帮我。
我使用http://wordpress.org/extend/plugins/advanced-custom-fields/作为主题选项/函数。我想显示类别的帖子在主页使用类别名称。这是我的代码:
<!-- Start Category Posts -->
<div class="boxtype2">
<div class="boxtitle1"><?php the_field('cat_1'); ?></div>
<div class="boxposts">
<?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<ul class="boxul">
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<br class="clear">
</li>
</ul>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'fileketchup' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
<?php wp_reset_query() ?>
</div>
<div class="boxfooter"><a href="<?php echo home_url(); ?>/<?php the_field('view_cat1'); ?>">View more</a></div>
</div>
<!-- End Category Posts -->请查一下<?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?>这个..。
在这里,我使用了类别名的预定义字段元插入。但它的表现错误..。我怎么解决的??如果我使用(“类别_名称=博客”,'showposts=5'),那么它就成功了。
发布于 2013-12-29 16:34:32
当你这样做时:
<?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?>字符串参数实际上是category_name=<?php the_field('cat_1_name'); ?>','showposts=5。好吧,如果字符串也没有触发致命错误的话。
你所拥有的是(假设我没有忘记混乱):
'category_name=<?php the_field('cat_1_name'); ?>','showposts=5','showposts=5'那是行不通的。如果要编写代码,就需要学习如何在PHP中创建字符串。这必须是你应该学会做好的前五件事。
其次,您不在HTML上下文中,因此您根本不需要使用PHP和close标记,这样做会引发进一步的致命错误。
第三,将查询类字符串语法与array语法混为一谈,这种语法在WordPress中神秘地流行。
第四,the_field echos的内容。无论如何,你都不能用它来连接字符串。你需要get_field。
第五,showposts已经被废弃很长一段时间了。使用posts_per_page。
应该工作的字符串:
"category_name=".get_field('cat_1_name')."&posts_per_page=5" 'category_name='.get_field('cat_1_name').'&posts_per_page=5'
但是不要使用查询字符串语法,创建一个数组。
$args = array(
'category_name' => get_field('cat_1_name'),
'posts_per_page' => 5
);应该注意的是,使用它来替换页面上的主要查询会增加页面加载时间,在最坏的情况下,需要的工作量增加一倍以上。在易于使用的同时,该功能也容易出现混淆和问题。详情请参阅下文关于注意事项的说明。http://codex.wordpress.org/Function_参考/查询_帖子 (重点地雷)
https://wordpress.stackexchange.com/questions/127940
复制相似问题