我想从我的WordPress博客上的标签中列出“n”个帖子(按最新排名排序)——帖子标题链接到帖子的永久链接。我还想为每个链接分配自己的类或ID。
我查过这个文件WordPress Codex, 这与我的需求非常相关,但对我来说意义不大(因此我无法根据我的需求修改它)。
希望我足够清楚,如果不清楚,我可以尝试澄清。
EDIT: 很明显,我希望能够在任何地方列出帖子。任何模板。
我想从我的WordPress博客上的标签中列出“n”个帖子(按最新排名排序)——帖子标题链接到帖子的永久链接。我还想为每个链接分配自己的类或ID。
我查过这个文件WordPress Codex, 这与我的需求非常相关,但对我来说意义不大(因此我无法根据我的需求修改它)。
希望我足够清楚,如果不清楚,我可以尝试澄清。
EDIT: 很明显,我希望能够在任何地方列出帖子。任何模板。
我不懂PHP,所以在向您展示了执行问题所需的PHP代码示例后,我将尝试进行解释。
CODE:
<!-- Featured Posts -->
<div id="featured-posts">
<?php
$count = 0;
$some_featured_posts = new WP_Query(array(\'tag\' => \'example2\', \'posts_per_page\' => 7));
while ($some_featured_posts->have_posts()):
$some_featured_posts->the_post();
$count++;
?>
<div class="featured-posts-wrapper<?php echo $count; ?> featured-posts-wrapper<?php echo ($count == 7) ? \' no-margin-right\' : \'\'; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img id="featured<?php echo $count; ?>" class="featured" alt="<?php the_title_attribute(); ?>" src="<?php echo get_post_meta($post->ID, \'image\', true); ?>" /></a>
<a id="featured-text<?php echo $count; ?>" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
<!-- Featured Posts -->
PS: 我已经加入了div标签和类,希望它们能帮助那些无知的人。因此,首先将代码放入模板中,并检查其输出方式和内容,以获得更好的想法。EXPLANATION:
<在您选择的任何模板中添加代码(header.php、index.php、home.php、archive.php、search.php、content.php或任何其他模板)。这就是您将看到输出的地方。更改号码(7
) 这一行中的数字等于您希望它显示的帖子数:
$some_featured_posts = new WP_Query(array(\'tag\' => \'example2\', \'posts_per_page\' => 7));
在上述代码中\'tag\' => \'example2\'
表示将显示标记为“example2”的最新帖子。如果要使用类别,请使用\'category_name\' => \'example2\'
这一行:
<div class="featured-posts-wrapper<?php echo $count; ?> featured-posts-wrapper<?php echo ($count == 7) ? \' no-margin-right\' : \'\'; ?>">
我使用class="featured-posts-wrapper<?php echo $count; ?>
这样我就可以为每个输出的帖子创建一个不同的类。具体而言,上述代码输出为class="featured-posts-wrapper1"
对于第一个帖子,class="featured-posts-wrapper2"
第二篇文章等等。另一方面<?php echo ($count == 7) ? \' no-margin-right\' : \'\'; ?>
允许您为最后一个条目设置特定类(如第7篇文章所示)。上述代码添加no-margin-right
类到输出中的最后一篇文章。
这一行:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img id="featured<?php echo $count; ?>" class="featured" alt="<?php the_title_attribute(); ?>" src="<?php echo get_post_meta($post->ID, \'image\', true); ?>" /></a>
<?php echo get_post_meta($post->ID, \'image\', true); ?>
使用“图像”自定义字段及其值(图像URL)输出指定给帖子的图像URL。我使用的其余值都很标准。<?php the_permalink(); ?>
输出永久链接URL,<?php the_title_attribute(); ?>
和<?php the_title(); ?>
输出帖子的标题。
这里有一个你可以完成的例子(你实际上可以做任何事情):
get_posts()
用于“次要”循环-存在于主循环旁边的循环,通常位于侧栏或底部列出“相关”帖子等。get_posts()
从模板页内调用。所以在这里使用它是错误的。
您将需要更改主查询,如中所述:When to use WP_query(), query_posts() and pre_get_posts
add_action(\'pre_get_posts\',\'wpse50857_alter_query\');
function wpse50857_alter_query($query){
//Check other conditionals depending on when you want to alter the loop
if( $query->is_main_query() && is_home() ){
$tax_query = array(array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'my-tag\'
));
$query->set(\'tax_query\',$tax_query);
$query->set(\'posts_per_page\',5);
}
}
请注意,这比以前任何一次都重要tax_query
(在这种情况下,这并不重要,但如果应该的话,你可以得到tax_query
并将其与新的合并…)Other conditionals available here.
要编辑帖子的显示方式,您需要编辑相应的模板页面(home.php
?, front-page.php
?). 循环将类似于:
if( have_posts() ):
while( have_posts() ): the_post();
//The loop
?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php
endwhile;
endif;
你可以随意编辑显示内容。我正在尝试添加自定义列以编辑标记。php,用于我创建的自定义分类法。这种自定义分类法称为设备类型,它仅适用于一种称为设备的自定义post类型。我正在尝试使用与在编辑中添加自定义列相同的过滤器和操作。php页面。我能够使用此筛选器实际创建自定义列:add_filter(\'manage_edit-equipment-types_columns\', \'define_equip_types_columns\'); 然而,我似乎无法用任何数据填充这些列。通常,用于编辑。php页面我将使用以下操作:a