简体   繁体   English

WordPress自定义类别字段返回匹配的帖子类别

[英]Wordpress custom category field returns matching post categories

So, I have the following query to show posts on a page template. 因此,我有以下查询以在页面模板上显示帖子。

        $wp_query = new WP_Query(); $wp_query->query('category_name=specials&posts_per_page=2' . '&paged='.$paged);

Where is have category_name=specials I'd like specials to be a dynamic field informed/taken from a custom field ON the page... so the page might have a custom field 'category' and it's value I can type in to be specials for example. 我想让Specials成为动态字段,该动态字段是从页面上的自定义字段通知/获取的,所以category_name = specials ...因此页面可能具有自定义字段“ category”,并且可以输入其值作为Specials例如。 Therefore the page will show all posts with the category matching what I've typed in the customer field value... Is this possible? 因此,该页面将显示所有类别与我在客户字段值中键入的内容匹配的帖子。这可能吗?

Yes, assign the value of the custom field to a variable and use it in the query as shown below: 是的,将定制字段的值分配给变量,并在查询中使用它,如下所示:

$custom_field = //get custom field value

$wp_query = new WP_Query(); $wp_query->query('category_name='.$custom_field.'&posts_per_page=2' . '&paged='.$paged);

How to get the category wise post by using below code? 如何通过使用下面的代码获取类别明智的职位?

Add category id and taxonomy slug in argument 在参数中添加类别ID和分类标准

    <?php
$post_type = 'post';
$page_paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'type'          => $post_type,
    'post_status'   => 'publish',
    'posts_per_page' => 6,
    'paged'          => $page_paged, 
    'caller_get_posts' => -1,
    'orderby'       => 'name',
    'order'         => 'DESC',
    'pad_counts'    => false,
    'hierarchical'  => 1,
    'hide_empty'    => 0,
    'tax_query'                => array(
        array(
            'taxonomy' => 'your taxonomy slug',
            'field' => 'id',
            'terms' => 'your category id'
        )
    ),
);

$loop = new WP_Query( $args );

while ( have_posts() ) : the_post();
    the_title('<h2>', '</h2>', true);
    the_content();
endwhile; // end of the loop. 

wp_reset_query();
?>

Thanks to Neil for a quick answer 感谢Neil的快速解答

Here is the full working block of code. 这是完整的代码块。

On the page, add a custom field 'category' then make sure the value of this custom field matches post categories which will return these posts on the page. 在页面上,添加一个自定义字段“ category”,然后确保此自定义字段的值与帖子类别匹配,这将在页面上返回这些帖子。

<?php // Display blog posts with category filter from custom field
    $temp = $wp_query; $wp_query= null;
    $custom_field = get_post_meta( get_the_ID(), 'category', true);
    $wp_query = new WP_Query(); $wp_query->query('category_name='.$custom_field.'&posts_per_page=2' . '&paged='.$paged);

    $wp_query = new WP_Query(); $wp_query->query('category_name=specials&posts_per_page=2' . '&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>

    <?php endwhile; ?>

    <?php if ($paged > 1) { ?>

    <nav id="nav-posts">
        <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
        <div class="next"><?php previous_posts_link('Newer Posts &raquo;'); ?></div>
    </nav>

    <?php } else { ?>

    <nav id="nav-posts">
        <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
    </nav>

    <?php } ?>

    <?php wp_reset_postdata(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM