Php 如何在WordPress自定义帖子类型中按类别显示帖子
朋友们好,我被困在一个密码里。在这里,我创建了一个自定义帖子类型,并在页面模板中调用了它的类别。我使用了下面的代码来显示帖子类型的类别Php 如何在WordPress自定义帖子类型中按类别显示帖子,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,朋友们好,我被困在一个密码里。在这里,我创建了一个自定义帖子类型,并在页面模板中调用了它的类别。我使用了下面的代码来显示帖子类型的类别 <?php $args = array( 'type' => 'post', 'child_of' => 0, 'pare
<?php
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'readings_post-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
<?php
}
echo '</ul>';
?>
现在,当我点击一个类别时,它会显示404错误页面,而不会显示我的帖子。请帮我做这件事。我用下面的代码创建了一个自定义帖子类型
/* readings Post Type */
if ( !function_exists('readings_category_register') ) {
function readings_category_register() {
$readings_permalinks = get_option( 'readings_permalinks' );
$args = array(
"label" => __('Topics'),
"singular_label" => __('Topic'),
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array(
'slug' => empty( $readings_permalinks['category_base'] ) ? __( 'readings-category' ) : __( $readings_permalinks['category_base'] ),
'with_front' => false
),
'query_var' => true
);
register_taxonomy( 'readings-category', 'readings', $args );
}
add_action( 'init', 'readings_category_register' );
}
/* readings POST TYPE
================================================== */
if ( !function_exists('readings_register') ) {
function readings_register() {
$readings_permalinks = get_option( 'readings_permalinks' );
$readings_permalink = empty( $readings_permalinks['readings_base'] ) ? __( 'readings' ) : __( $readings_permalinks['readings_base'] );
$labels = array(
'name' => __('readings'),
'singular_name' => __('readings'),
'add_new' => __('Add New readings'),
'add_new_item' => __('Add New readings'),
'edit_item' => __('Edit readings'),
'new_item' => __('New readings'),
'view_item' => __('View readingss'),
'search_items' => __('Search readings'),
'not_found' => __('No readingss have been added yet'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'menu_icon'=> 'dashicons-groups',
'rewrite' => $readings_permalink != "readings" ? array(
'slug' => untrailingslashit( $readings_permalink ),
'with_front' => false,
'feeds' => true
)
: false,
// 'supports' => array('title', 'editor'),
'show_in_rest' => true,
'supports' => array('title','editor', 'author', 'thumbnail', 'excerpt', 'comments','categories'),
//"taxonomies" => [ "readings-category" ],
'has_archive' => true,
'taxonomies' => array('readings-category')
);
register_post_type( 'readings', $args );
}
add_action( 'init', 'readings_register' );
}
/* readings POST TYPE COLUMNS
================================================== */
if ( !function_exists('readings_edit_columns') ) {
function readings_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => __("readings Name"),
"description" => __("readings Description"),
"readings-category" => __("readings Category")
);
return $columns;
}
add_filter("manage_edit-readings_columns", "readings_edit_columns");
}
/*阅读文章类型*/
如果(!function_存在('reads_category_register')){
功能读数\类别\寄存器(){
$readings\u permalinks=获取选项(“readings\u permalinks”);
$args=数组(
“标签”=>(主题),
“单数标签”=>(“主题”),
“public”=>正确,
“分层”=>正确,
'show_ui'=>true,
“在导航菜单中显示”=>false,
'args'=>array('orderby'=>term_order'),
“重写”=>数组(
“slug”=>空($readings\u permalinks['category\u base'])?\uuuuuuuu('readings category'):\uuuuuuuuu($readings\u permalinks['category\u base']),
'带_front'=>假
),
“查询变量”=>true
);
注册分类法(‘阅读分类’、‘阅读’、$args);
}
添加动作('init','reads'u category'u register');
}
/*阅读文章类型
================================================== */
如果(!函数_存在('reads_register')){
函数读数\u寄存器(){
$readings\u permalinks=获取选项(“readings\u permalinks”);
$readings\u permalink=空($readings\u permalinks['readings\u base'])?\uuuuu('readings'):\uuuuuuu($readings\u permalinks['readings\u base']);
$labels=数组(
“名称”=>“‘读数’”,
“单数名称”=>“(读数”),
“添加新读数”=>“(添加新读数”),
“添加新项目”=>“(添加新读数”),
“编辑项目”=>“‘编辑读数’”,
“新建项目”=>“(新建读数”),
“查看项目”=>“‘查看读数’”,
“搜索项目”=>“(“搜索读数”),
'not_found'=>'(尚未添加任何读数),
“未在垃圾桶中找到”=>“‘垃圾桶中找不到任何东西’,
“父项冒号”=>“
);
$args=数组(
“标签”=>$labels,
“public”=>正确,
'show_ui'=>true,
“在菜单中显示”=>true,
“在导航菜单中显示”=>true,
“菜单图标”=>“仪表盘图标组”,
“重写”=>$reads\u permalink!=“reads”?数组(
“slug”=>未跟踪的lashit($reads\u permalink),
'with_front'=>false,
“提要”=>true
)
:错,
//'支持'=>数组('title','editor'),
“在休息中显示”=>正确,
'支持'=>数组('标题','编辑','作者','缩略图','摘录','评论','类别'),
//“分类法”=>[“阅读分类”],
“has_archive”=>true,
“分类法”=>array('reads-category')
);
寄存器后置类型('readings',$args);
}
添加操作('init','reads_register');
}
/*阅读柱型栏
================================================== */
如果(!function_存在('reads_edit_columns')){
函数读取\编辑\列($columns){
$columns=数组(
“cb”=>“”,
“标题”=>“(“名称”),
“说明”=>“读数说明”,
“阅读资料类别”=>(阅读资料类别)
);
返回$columns;
}
添加_过滤器(“管理_编辑-读数_列”、“读数_编辑_列”);
}
试试这个
$args = array(
'posts_per_page' => -1,
'post_type' => 'here write post name',
'orderby' => 'date',
'order' => 'DESC',
'category_name' => 'here write category name'
);
$loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
...
<?php endwhile; ?>
$args=array(
“每页帖子数”=>-1,
“帖子类型”=>“在这里写帖子名”,
'orderby'=>'date',
“订单”=>“描述”,
“类别名称”=>“在此处写入类别名称”
);
$loop=新的WP_查询($args);
...
Hi@dipak Dholakiya,我的代码正确地显示了所有类别,但当我单击类别时,其页面没有显示。你能告诉我需要哪种类型的页面吗?比如:分类的单一posttype或分类posttype=>category.phpCustom post type单一页面是=>single-post\u type\u name.phpCan你能告诉我你想要什么吗@harsimransingharimran.singh1988这是我的Skyped这能回答你的问题吗?