Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 修改全局wp_查询以返回自定义帖子类型的最新版本_Wordpress - Fatal编程技术网

Wordpress 修改全局wp_查询以返回自定义帖子类型的最新版本

Wordpress 修改全局wp_查询以返回自定义帖子类型的最新版本,wordpress,Wordpress,我开发了一个带有自定义帖子类型的主题。其中一种post_类型称为事件 我不想在名为“即将到来的活动”的“页面”中显示最新的活动。所以我创建了这个名为“事件”的模板文件。现在我需要修改全局wp_查询,以便返回最后一个事件 这就是我的single.php的样子: <?php get_header(); setup_postdata($post); get_page_post_content(); get_footer(); ?> 当以自定义帖子类型“正常”查看事件时,效果良好。这是

我开发了一个带有自定义帖子类型的主题。其中一种post_类型称为事件

我不想在名为“即将到来的活动”的“页面”中显示最新的活动。所以我创建了这个名为“事件”的模板文件。现在我需要修改全局wp_查询,以便返回最后一个事件

这就是我的single.php的样子:

<?php
get_header();
setup_postdata($post);
get_page_post_content();
get_footer();
?>

当以自定义帖子类型“正常”查看事件时,效果良好。这是我的模板文件,其中应包含最新事件:

<?php
/*
 * Template Name: Event
*/
?>

<?php

global $wp_query;
$wp_query = new WP_Query('post_type=event&posts_per_page=1');

include 'single.php';

?>

但是,这并没有像预期的那样起作用,因为我的模板的其他部分取决于属性,例如“is_single()”,这种情况下返回false,因为查询是从页面调用的。我需要设置查询以便更改这些属性。有人知道我应该如何解决这个问题吗


谢谢!!:D

将下面的代码放入模板文件中

<?php
/**
Template Name: Event
 */

get_header();
?>
    <div id="content" >
    <?php

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    query_posts('post_type=event&paged='.$paged.'&posts_per_page=10');


    if (have_posts ()) :
        while (have_posts ()) : the_post(); ?>
            <div <?php post_class() ?>>
                <div class="post-title">
                    <h2 class="alignleft"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                </div><!-- .post-title -->

                <div class="post-content">
                        <?php the_excerpt('read more');?>
                </div>
            </div><!--end of .post_class -->

            <div class="post-meta">
                <p class="alignright"><a class="readmore" title="<?php get_the_title();?>" href="<?php echo get_permalink( $post->ID );?>" rel="nofollow">Read</a></p>
                <span class="alignright comment-cnt"><?php comments_popup_link( 'No Comments', '1 Comment', '% Comments' ); ?></span>
                <div class="clear"></div>
            </div><!-- end of .post-meta -->

        <?php
        endwhile;
    endif;
    ?>
    </div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>


将下面的代码放入模板文件中

<?php
/**
Template Name: Event
 */

get_header();
?>
    <div id="content" >
    <?php

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    query_posts('post_type=event&paged='.$paged.'&posts_per_page=10');


    if (have_posts ()) :
        while (have_posts ()) : the_post(); ?>
            <div <?php post_class() ?>>
                <div class="post-title">
                    <h2 class="alignleft"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                </div><!-- .post-title -->

                <div class="post-content">
                        <?php the_excerpt('read more');?>
                </div>
            </div><!--end of .post_class -->

            <div class="post-meta">
                <p class="alignright"><a class="readmore" title="<?php get_the_title();?>" href="<?php echo get_permalink( $post->ID );?>" rel="nofollow">Read</a></p>
                <span class="alignright comment-cnt"><?php comments_popup_link( 'No Comments', '1 Comment', '% Comments' ); ?></span>
                <div class="clear"></div>
            </div><!-- end of .post-meta -->

        <?php
        endwhile;
    endif;
    ?>
    </div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>