Php 如何在wordpress中自定义帖子?
我尝试使用wordpress中的快捷码方式自定义我自己的帖子。我想按类别显示帖子Php 如何在wordpress中自定义帖子?,php,wordpress,Php,Wordpress,我尝试使用wordpress中的快捷码方式自定义我自己的帖子。我想按类别显示帖子 <?php function sample_post($atts) { extract(shortcode_atts(array( 'category_name' => 'uncategorized' ), $atts)); $args = array('category_name' => $category_name); query_posts($args)
<?php
function sample_post($atts) {
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<label><?php the_title(); ?></label>
<?php
endwhile;
endif;
}
add_shortcode('sample_post', 'sample_post');
?>
我不知道我是否错过了有关设置或查询的内容。这是我非常想展示的。我只想显示
(文章的标题),但现在它显示content-page.php。我也不明白为什么它会出现在我的页面上。我没有调用content-page.php来显示我的页面。更改代码以返回数据,如下所示
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');
函数示例\u post($atts){
$return='';
提取(短码)附件(数组)(
“类别名称”=>“未分类”
)美元(附件);;
$args=array('category\u name'=>$category\u name);
$thew_query=newwp_query($args);
if($the_query->have_posts()):while($the_query->have_posts()):$the_query->the_post();
$return.=''。标题();
结束时;
endif;
return$return;
}
添加_短码('sample_post','sample_post');
still content\u page.php正在运行。结果还是一样,问题看起来不一样。尝试使用WP_Query()将其关闭。我能给你举个例子吗?你能举个例子吗
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');