admin管理员组

文章数量:1122846

I inherited a website that had a significant number of customized PHP files. I have removed many since, however, this is one of the few that still seemed relevant. The code below displays 1 image at random from a particular post type (gallery) in the website footer.

There was only one post type category prior to me working on the website. Now I have added more gallery categories, but the footer is displaying images from all categories. How can I modify this to show a particular category from the gallery post type?

<?php query_posts('orderby=rand&showposts=1&post_type=gallery'); 
                        while ( have_posts() ) : the_post();?>
                                <script>
                    jQuery(document).ready(function(){
                        $voice = '<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "medium" ); echo $image[0]; ?>';
                        jQuery('.footer-voice').attr('src', $voice);
                    });
                </script>
                            
                        <?php endwhile; ?>

I inherited a website that had a significant number of customized PHP files. I have removed many since, however, this is one of the few that still seemed relevant. The code below displays 1 image at random from a particular post type (gallery) in the website footer.

There was only one post type category prior to me working on the website. Now I have added more gallery categories, but the footer is displaying images from all categories. How can I modify this to show a particular category from the gallery post type?

<?php query_posts('orderby=rand&showposts=1&post_type=gallery'); 
                        while ( have_posts() ) : the_post();?>
                                <script>
                    jQuery(document).ready(function(){
                        $voice = '<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "medium" ); echo $image[0]; ?>';
                        jQuery('.footer-voice').attr('src', $voice);
                    });
                </script>
                            
                        <?php endwhile; ?>
Share Improve this question asked Jul 15, 2020 at 21:43 NinjaValerieNinjaValerie 11 bronze badge 0
Add a comment  | 

2 Answers 2

Reset to default 0

The immediate answer to your question is that you can just add a category ID to the query_posts() call, e.g.:

query_posts('orderby=rand&showposts=1&post_type=gallery&cat=10');

Slightly longer answer is that it's not a good idea to use query_posts in themes and plugins. More info in the docs here: https://developer.wordpress.org/reference/functions/query_posts/

You might want to take the opportunity to change the code to use get_posts or WP_Query, you just need to put the arguments in an array instead of a query string.

I assume there's a custom taxonomy registered for your custom post type. In this case you need to use the tax_query parameter for WP_Query to get posts from your custom taxonomy with a certain term slug.

Here's an example code for getting custom post type post with a custom term. First there's a helper function, which you can put in your functions.php file. The lines below it demonstrates how to use the function. You need to update the taxonomy name to match the actual one to make the query work.

The category_name parameter is related to the Category taxonomy, which is associated with the Post post type. If your gallery post type also uses the default categories as its taxonomy, then use category as the taxonomy below.

// in functions.php
function get_random_gallery_post_by_term( string $term ) {
    // Setup args
    $args = array(
        'post_type'      => 'gallery',
        'post_status'    => 'publish',
        'posts_per_page' => 1,
        'orderby'        => 'rand',
        'tax_query'      => array(
            // Query post by custom taxonomy term
            array(
                // update your_gallery_taxonomy_name to match your actual taxonomy name
                // You can find taxonomy name on your browser address line when you're on the taxonomy admin page
                'taxonomy'     => 'your_gallery_taxonomy_name', 
                'field'        => 'slug',
                'terms'        => $term
            )
        ),
    );
    // Execute query
    $query = new WP_Query( $args );
    // Return found post or false, if not found
    return $query->posts ? $query->posts[0] : false;
}

// in some template file
$gallery_post = get_random_gallery_post_by_term( 'my-gallery-category' );
// Do something with the post
if ( $gallery_post ) {
    // Thumbnail, empty string if thumbnail not found
    echo get_the_post_thumbnail( $gallery_post->ID, 'thumbnail' );
    // Content
    echo wp_kses_post( $gallery_post->post_content );
}

本文标签: phpDisplay featured image of post type category