0

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!

4

2 回答 2

2

编辑 pre_get_posts 将替换原始查询,您将根本没有页面的内容。如果您只想显示投资组合帖子类型的内容而不是投资组合页面的内容,我只会建议您采用这种方法。

对于一般的帖子查询,建议使用 WP_Query 或 get_posts。

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

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

如果您使用 WP_Query 函数 wp_reset_postdata() 会将帖子数据恢复为原始数据,以便您可以获取原始页面的内容。

$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();

现在您将能够使用原始循环来显示页面的内容

<?php while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
于 2013-10-18T19:57:50.137 回答
0

通常,我将查询帖子放在一个变量中,如下所示:

$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); 

然后你可以像这样循环它:

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

最后,要访问您的页面内容,您可以使用变量$post,它是由 wordpress 自动设置的。无需添加更多代码即可访问您的页面内容。

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

页面内容的 foreach 循环有点矫枉过正,并且有更好的方法(至少很可能),但我还没有费心去进一步研究它!它虽然有效!

于 2013-10-18T19:39:12.010 回答