GVKun编程网logo

如何按帖子类型中的类别计算帖子?(如何按帖子类型中的类别计算帖子数量)

22

在这篇文章中,我们将带领您了解如何按帖子类型中的类别计算帖子?的全貌,包括如何按帖子类型中的类别计算帖子数量的相关情况。同时,我们还将为您介绍有关php–WordPress通过帖子类型订购帖子、php

在这篇文章中,我们将带领您了解如何按帖子类型中的类别计算帖子?的全貌,包括如何按帖子类型中的类别计算帖子数量的相关情况。同时,我们还将为您介绍有关php – WordPress通过帖子类型订购帖子、php – WordPress:如何仅显示特定类别的帖子?、php – WordPress:检查特定自定义帖子类型中是否存在slug、php – WP REST API从帖子类型中获取帖子的知识,以帮助您更好地理解这个主题。

本文目录一览:

如何按帖子类型中的类别计算帖子?(如何按帖子类型中的类别计算帖子数量)

如何按帖子类型中的类别计算帖子?(如何按帖子类型中的类别计算帖子数量)

如何解决如何按帖子类型中的类别计算帖子??

我已经尝试过这个代码:

$cats = get_terms(''category'');
$counts_by_term = wp_list_pluck( $cats,''count'',''slug'' );
echo $counts_by_term[''uncategorized''];

但问题在于,每个帖子类型都共享类别分类法。

情况:

我有一个名为 Testimonials 的 CPT,并启用了该类别并在那里创建了一个未分类类别的帖子。 我也有 5 个帖子类型的帖子,所有帖子都分配了未分类的类别。

现在,我只想从推荐帖子类型中获取未分类类别的帖子数量。

上面的代码会输出:6

应该是:1

解决方法

这是我的解决方案,

$cat_id = 1;
$post_type = ''testimonials''; 
$args = array(
    ''numberposts'' => -1,''category'' => $cat_id,''post_type'' => $post_type,);
$count_posts = get_posts( $args );
$total_posts = count($count_posts);
echo $total_posts;
,

//这会减轻数据库的压力

kafka-acls

php – WordPress通过帖子类型订购帖子

php – WordPress通过帖子类型订购帖子

我正在尝试创建一个系统,从2个帖子类型中提取帖子,我想按帖子类型显示帖子顺序.我试图首先显示一个帖子类型的帖子,然后另一个帖子类型,但我使用的查询混合了所有.任何人都可以建议一个很好的解决方案.这是我的代码.

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
 $args = array(

     'post_type' => array('review','directory'),
     'orderby' => 'name',
     'order' => 'ASC',
     'posts_per_page'  => '4',
     'paged'          => $paged ,
     'tax_query' => array( array(
        'taxonomy' => 'Reviews',
        'field' => 'id',
        'terms' => $cat_id
    ), ),
     );

 query_posts($args);
        if (have_posts()) :
       while (have_posts()) : the_post();
      $product_terms = wp_get_post_terms(get_the_ID(), 'Reviews', array("fields" => "all", "order" => "DESC"));
      $postype=get_post_type( get_the_ID() );
      if($postype=='review')
      {
     ?>   

        <div>
          <a href="http://<?PHP the_permalink(); ?>" target="_blank"><h3><?PHP the_title(); ?></h3></a>
          <div>
            <?PHP echo get_the_term_list( $post->ID, 'Reviews', 'Category: ', ', ', '' ); ?>
          </div>

          <?PHP
          if(get_field('see_it'))
              $seeit_text= get_field('see_it');
         if(get_field('skip_it'))
              $skipit_text= get_field('skip_it');
         ?>
          <div>
            <p><span>See it:</span>
                <?PHP echo $seeit_text; ?>
            </p>
            <p><span>Skip it:</span>
                <?PHP echo $skipit_text;  ?>
            </p>
          </div>
          <?PHP echo custom_field_excerpt(); ?>
        </div>
          <?PHP }
          else
          {
              ?>
                <div>
          <a href="h<?PHP the_permalink(); ?>" target="_blank"><h3><?PHP the_title(); ?></h3></a>
          <div>
            <?PHP echo get_the_term_list( get_the_ID(), 'Reviews', 'Category: ', ', ', '' ); ?>
          </div>


          <?PHP echo the_field('enter_content_direc'); ?>
          <?PHP
          if(get_field('enter_textdirec'))
              $text= get_field('enter_textdirec');
         if(get_field('enter_linkdirec'))
              $textlink= get_field('enter_linkdirec');
         ?>
          <div>
            <p><span><a  target="_blank"href="<?PHP echo $textlink;?>"><?PHP echo $textlink;?>  </a></span>

            </p>
          </div>
        </div>
          <?PHP }
          endwhile;
                  echo '<div>';
                 wp_pagenavi();
                 echo '</div>';
                endif;

那么有没有办法让我首先显示帖子形式评论然后目录?

解决方法:

最后我得到了解决方案,可以使用过滤器完成.

解决方案看起来像这样.

add_filter( 'posts_request' , 'modify_request' );
function modify_request( $query) {
    global $wpdb;
    if(strstr($query,"post_type IN ('review', 'directory')")){
        $where = str_replace("ORDER BY {$wpdb->posts}.post_date","ORDER BY {$wpdb->posts}.post_type",$query);
    }
    return $where;
}

php – WordPress:如何仅显示特定类别的帖子?

php – WordPress:如何仅显示特定类别的帖子?

我是wordpress的新手,但花了大约50个小时的时间来研究它,尝试一下这样的事情,感觉我现在已经有了很好的处理能力.

然而,我根本无法工作的一件事是让一个页面吐出某个类别的帖子列表.

这是我的例子:http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters

我有一个帖子,如果我直接使用它可以正常工作,但不会显示在此页面上.

The post direct link.

类别ID为“3”,而类别名称为“印刷海报”.

我有一个印刷海报页面的自定义页面模板,如下所示:

<?PHP
/*
Template Name: Typographic Posters
*/
?>

<?PHP get_header(); ?>
<?PHP get_sidebar(); ?>

<?PHP if (in_category('3')): ?>
<div>

    <?PHP if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


  <div <?PHP post_class() ?> id="post-<?PHP the_ID(); ?>">
   <div>
    <h2><?PHP the_title(); ?></h2>
    <?PHP the_content(); ?>
   </div>
   <?=get_image('Flutter-image');?>
  </div>


    <?PHP endwhile; else: ?>
     <p><?PHP _e('Sorry, no posts matched your criteria.'); ?></p>
    <?PHP endif; ?>

</div>
<?PHP endif; ?>

<?PHP get_footer(); ?>

然而,使用此代码只显示页面获取标题,侧边栏和其他内容.

如果有人可以帮助我,那将真正帮助我处理wordpress类别的过滤.

谢谢阅读,

Jannis

解决方法:

in_category只能在单个页面上的循环外工作.我建议使用query_posts函数来解决这个问题.您可以使用query_posts(‘cat = 3’)或query_posts(‘category_name = typographic-posters’)来获取您要查找的帖子.

获得后,只需使用普通的wordpress循环即可访问这些帖子.

php – WordPress:检查特定自定义帖子类型中是否存在slug

php – WordPress:检查特定自定义帖子类型中是否存在slug

我正在使用下面的代码来检查slug是否存在,但它正在搜索所有帖子类型,我只需要检查特定的自定义帖子类型.

function the_slug_exists($post_name) {
    global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
        return true;
    } else {
        return false;
    }
}

用法:

if (the_slug_exists($term)) :
    echo 'Ok';
endif;

是否可以修改此代码以仅搜索特定的自定义帖子类型?

解决方法:

function the_slug_exists($post_name, $post_type) {
    global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'", 'ARRAY_A')) {
        return true;
    } else {
        return false;
    }
}

用法

if (the_slug_exists($term,$type)) :
    echo 'Ok';
endif;

php – WP REST API从帖子类型中获取帖子

php – WP REST API从帖子类型中获取帖子

如何使用WP REST API(v1或v2)获取特定自定义帖子类型的所有帖子?我对此非常陌生,并试图了解如何做到这一点.

我目前正在使用WP REST API v2并设法使用此获取所有帖子类型的列表

http://domain.com/wp-json/wp/v2/types

然后设法得到我感兴趣的帖子类型

http://domain.com/wp-json/wp/v2/types/the-icons-update

如何从特定内容类型中获取所有帖子?

我试过了

http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update

但它返回一个空数组(我想它会返回默认帖子,而在我的网站上只有我试图检索的自定义帖子类型中的帖子).

我如何注册帖子类型会有问题吗?

function custom_post_type() {
$labels = array(
    'name'               => _x( 'The Icons Update','post type general name' ),'singular_name'      => _x( 'The Icons Update','post type singular name' ),'add_new'            => _x( 'Add Page','magazine' ),'add_new_item'       => __( 'Add New Page' ),'edit_item'          => __( 'Edit Page' ),'new_item'           => __( 'New Page' ),'all_items'          => __( 'All Pages' ),'view_item'          => __( 'View Page' ),'search_items'       => __( 'Search Pages' ),'not_found'          => __( 'No Page found' ),'not_found_in_trash' => __( 'No Page found in the Trash' ),'parent_item_colon'  => '','menu_icon'          => '','menu_name'          => 'The Icons Update'
);
$args = array(
    'labels'        => $labels,'description'   => 'Holds our projects and project specific data','public'        => true,'menu_position' => 5,'supports'      => array( 'title','editor','thumbnail','excerpt','custom-fields' ),'has_archive'   => true,'taxonomies'    => array('post_tag','category'),'hierarchical'  => false,'query_var'     => true,'queryable' => true,'searchable'    => true,'rewrite'       => array( 'slug' => 'the-icons-update' )
);
register_post_type( 'magazine',$args );
flush_rewrite_rules();
}
add_action( 'init','custom_post_type' );

任何帮助都非常感谢.

对于v.2,有一个非常简单的方法.您所要做的就是在args数组中包含以下属性:’show_in_rest’=>真正

例:

register_post_type( 'recipe',array(
          'labels' => $labels,'public' => true,'hierarchical' => false,'supports' => $supports,'show_in_rest' => true,'taxonomies' => array('recipe-type','post_tag'),'rewrite' => array( 'slug' => __('recipe','recipe') )
     )
);

今天关于如何按帖子类型中的类别计算帖子?如何按帖子类型中的类别计算帖子数量的介绍到此结束,谢谢您的阅读,有关php – WordPress通过帖子类型订购帖子、php – WordPress:如何仅显示特定类别的帖子?、php – WordPress:检查特定自定义帖子类型中是否存在slug、php – WP REST API从帖子类型中获取帖子等更多相关知识的信息可以在本站进行查询。

本文标签: