Show message as shown if taxonomy matches current category

I used ACF. I added a taxonomy drop-down box to the post edit page. From the drop-down list, I choose in which category this post should be placed, but in my code the same post is displayed as in all categories.

Below is the code in the category.php file. I need it to display the most recent entry that has been assigned the "Feature In Category", and therefore it should appear in the category that I defined.

enter image description here

My current loop in .php category

<?php 

$category = get_field('feature_in_category');

// args
$args = array(
    'numberposts' => -1,
    'posts_per_page' => 1,
    'category__in' => $category,
    'orderby'=> 'modified'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>


<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="small-6 columns">
        <div class="img-box-shadow">
            <a href="<?php the_permalink(); ?>">
                <?php echo the_post_thumbnail(); ?>
            </a>
        </div>
    </div>
    <div class="small-6 columns">
        <h3><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h3>
        <p><?php echo the_excerpt(); ?></p>
    </div>


<?php endwhile; ?>

<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>

<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Pastebin: http://pastebin.com/NR3UanAd

+4
source share
2 answers

ACF . .., :

ACF TAXONOMY FIELD

ACF NULL , , get_field, , . , get_field, , , , .

ACF

$field = get_field($field_name, $post_id, $format_value); 

$post_id: , . ( ). ///etc

ACF

, ACF, , ACF , . , key=>value. meta_query

ACF , . , , , slug ID, , , ,

, , , . get_queried_object_id()

, , meta_key=feature_in_category , key=>value

, ACF , ( PHP5.4 +, [] array())

$cat_id = get_queried_object_id(); // Assumption that category ID is saved by ACF

$args = [
    'posts_per_page' => 1,
    'orderby'        => 'modified',
    'meta_key'       => 'feature_in_category',
    'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
];
$q = new WP_Query( $args );

if( $q->have_posts() ) {
    while( $q->have_posts() ) {
        $q->the_post();

        // YOUR TEMPLATE TAGS AND MARKUP

    }
    wp_reset_postdata();
}

ACF, slug,

$category = get_queried_object()->name; // For category name

$category = get_queried_object()->slug; // For category slug

'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF

'meta_value' => $category, 

, , OP, .

$cat_id = get_queried_object_id();
$args = [
        'posts_per_page' => 1,
        'orderby'        => 'modified',
        'meta_query' => [
                [                        
                        'key' => 'feature_in_category',
                        'value' => $cat_id,
                        'compare' => 'IN'
                ]
        ]
];
+2

http://codex.wordpress.org/Class_Reference/WP_Query , . , , . , :

<?php 

$category = get_field('feature_in_category');

// args
$args = array(
    'numberposts' => -1,
    'posts_per_page' => 1,
    'category__in' => $category,
    'orderby'=> 'modified'
);

// get results
$the_query = new WP_Query( $args );
echo '<pre>'; print_r($the_query); die();

, , - phpMyAdmin Navicat . , , . , . , , .

UPDATE: , "cat", "category__in". , , , get_field. : http://www.the-perfect-life.org/html-5-css-3-php-wordpress-jquery-javascript-photoshop-illustrator-tutorial/how-to-create-custom/wordpress-plugin-theme-codex-code-function/advanced-custom-fields-get-field-taxonomy-query-related-posts-by-tag-and-category/

$categoryValue = get_field('feature_in_category');
$args=array(
'cat' => $categoryValue,
'posts_per_page'=>1 // Number of related posts to display
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div>
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
wp_reset_query();
0

Source: https://habr.com/ru/post/1569290/


All Articles