Pregunta

I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.

So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:

<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>

What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:

<?php while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; // end of the loop. ?>

to get content from the actual page.

This is what I am trying to do, I've replaced:

query_posts('post_type=portfolio&posts_per_page=10');

with:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_page( 8 ) && $query->is_main_query() )
        $query->set( 'post_type', array( 'portfolio' ) );
    return $query;
}

This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.

Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.

Thank you!

¿Fue útil?

Solución

Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.

For general post queries it is recommended to use WP_Query or get_posts.

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

http://codex.wordpress.org/Template_Tags/get_posts

If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.

$args = array(
    'posts_per_page' => 10,
    'post_type' => 'portfolio',

);    

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

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Now you will be able to use the original loop to show the content of your page

<?php while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; // end of the loop. ?>

Otros consejos

Usually, I stick my query posts in a variable, like so:

$catid = get_cat_ID('My Category Name');

$args = array(
    'posts_per_page' => 5,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'category' => $catid
);

$posts_array = get_posts($args); 

Then you can loop it like so:

<?php foreach ($posts_array as $post) : setup_postdata($post);?>
    <h1><?php the_title(); ?></h1>
    <p><?php the_content(); ?></p>
<?php endforeach; ?>

Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.

<?php foreach( $posts as $post ) : setup_postdata($post); ?>
    <h1><?php the_title(); ?></h1>
    <p><?php the_content(); ?></p>
<?php endforeach; ?>

The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top