最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Querying & displaying custom post type into an existent page

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I am creating a WordPress site, with the Avada theme, for which I need to use Custom Post Type.

For that, I installed the extensions CPT UI & ACF Pro which allowed me to create my Custom Post Type (call the "Movie" for the example) with its fields which go well (ex: "releaseDate", "minimumAgeFor").

My wish is to be able to integrate in an existing page (done with Fusion Builder provided by the Avada Theme), a section displaying the list of these movies with the possibility to make the layout that goes well (flexbox ...).

My question is to know the cleanest way to integrate within this existing page the piece of PHP code that will allow me to search for movies and display them as I want?

I read that it was possible to define your own page to list the CPT but my wish is to display them in an existing page.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I am creating a WordPress site, with the Avada theme, for which I need to use Custom Post Type.

For that, I installed the extensions CPT UI & ACF Pro which allowed me to create my Custom Post Type (call the "Movie" for the example) with its fields which go well (ex: "releaseDate", "minimumAgeFor").

My wish is to be able to integrate in an existing page (done with Fusion Builder provided by the Avada Theme), a section displaying the list of these movies with the possibility to make the layout that goes well (flexbox ...).

My question is to know the cleanest way to integrate within this existing page the piece of PHP code that will allow me to search for movies and display them as I want?

I read that it was possible to define your own page to list the CPT but my wish is to display them in an existing page.

Share Improve this question edited Nov 7, 2019 at 15:35 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 7, 2019 at 15:09 RicouRicou 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can create a shortcode and add it in any page you want. This code will register your custom shortcode:

add_action( 'init', 'register_custom_shortcodes' );

function register_custom_shortcodes(){
    add_shortcode( 'my-custom-shortcode', 'display_movies' );
}

This code will be responsible for the content that will be displayed wherever you place the shortcode:

function display_movies(){

   ob_start();

   //Your code here to display your custom post type posts

   return ob_get_clean();

}

You can place the above functions to your functions.php

Then inside the function display_movies() you can run a WP_Query to display your posts. You can read about all the arguments that WP_Query takes here: https://developer.wordpress/reference/classes/wp_query/

A sample code to display your custom post type posts would be:

$args = array(
    'posts_per_page'    => 10, //Number of Posts
    'post_type'         => 'movie', //Replace with you custom post type slug
    'post_status'       => 'publish',
);


// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

    echo '<div class="movies-wrapper">';

    while ( $the_query->have_posts() ) {

        $the_query->the_post();
        ?>

        <div class="movie-item">

          <?php the_title(); ?>

          <?php

              //Maybe display a custom field
             echo get_post_meta( get_the_ID(), 'your_custom_meta_key', true );

           ?>

        </div>
        <?php
    }

    echo '</div>';

} 
else {
    // no posts found
}

// Restore original Post Data
// This code is important do not forget to place it
wp_reset_postdata();
wp_reset_query()

You can place the above code in your display_movies() function.

Once you have created your shortcode you can place it any page you want as following:

[my-custom-shortcode][/my-custom-shortcode]

As for the search functionality it becomes a bit more complicated. It depends on the filters you might want to be used, if you want an AJAX search. In most cases you will still use the WP_Query to achieve a search functionality.

发布评论

评论列表(0)

  1. 暂无评论