Отображать сообщения на основе имени категории CPT - PullRequest
0 голосов
/ 18 марта 2020

У меня есть тип поста с именем 'service'. Он имеет две категории «Левый» и «Правый» I sh для отображения всех сообщений категории «Левый». Вот мой код

<?php
    $args = array (
    'post_type'              => 'service',
    'post_status'            => 'publish',
    'order'                  => 'ASC',
    'category_name' => 'Left',
    'posts_per_page'=>-1
    );
    $posts = new WP_Query( $args );
    if ( $posts->have_posts() ) {

    while ( $posts->have_posts() ) {
    $posts->the_post();
    //$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

?>

    <li><a href="<?php the_permalink(); ?>"><i class="fa fa-file-text"></i> <?php the_title(); ?></a></li>

 <?php  } } wp_reset_postdata(); ?>

Приведенный выше код ничего не возвращает.

Ответы [ 2 ]

0 голосов
/ 18 марта 2020

Попробуйте это с именем категории

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category', //name of texonomy
            'field' => 'slug',
            'terms' => 'Left'
        )
    )
);
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();
        //your code
    endwhile;
endif;
0 голосов
/ 18 марта 2020

Пожалуйста, попробуйте с нижеуказанным:

В массиве $ * arg используйте Left идентификатор категории, состоящий из category_ID в "category" столбец.

<?php
$args = array (
    'post_type'  => 'service',
    'post_status' => 'publish',
    'order' => 'ASC',
    'category' => 'category_ID',
    'posts_per_page' =>-1
);

$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
    while ( $posts->have_posts() ) {
        $posts->the_post();
        //$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
        ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <i class="fa fa-file-text"></i> 
                <?php the_title(); ?>
            </a>
        </li>
        <?php  
    } 
} 
wp_reset_postdata();
?>
...