来自自定义元框的值在帖子中重复 [英] Values from custom meta boxes being repeated in posts

查看:16
本文介绍了来自自定义元框的值在帖子中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 WordPress 中的事件创建了自定义帖子类型,现在我试图将它们与来自我称为事件的页面模板上的自定义元框的值一起列出.

来自元框的值被重复,后面跟着一个模式:

  • 最新的帖子很好,

  • 旧帖子显示其元框值和来自最新帖子的值,

  • 等等.

这是正在发生的事情的截图:

这是一个显示帖子的查询:

<?php get_header();?><?php if (have_posts()) : while (have_posts()) : the_post();if ( get_post_meta($post->ID, '_ct_hdr_value') ) : ?><div class="page-name innerpage<?php echo $post->post_name; ?>"><div class="row"><div class="twelvecol"><h1 class="page-h1"><?php the_title();?>

<?php endif;?><div class="row"><div class="page-container"><div class="row"><div class="内容十二列"><?php echo the_content();终了;万一;?>

<section class="events cf"><h3 class="fancy"><span>即将举行的活动</span></h3><ul id="office-list" class="cf"><?phpquery_posts(array('post_type' => 'event', 'posts_per_page' => 30));if (have_posts()) : while (have_posts()) : the_post();?><li class="sixcol cf"><article class="event cf"><a class="cf" href="<?php echo the_permalink(); ?>"><h5 class="text-center"><?php the_title();?></h5></a><br><a class="cf" href="<?php echo the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' =>'img-center img-responsive event-thumb'));?></a><?php the_content() ?><section class="event-details"><section class="event-address cf"><?php$地址= $地址= $日期= $城市;如果(get_post_meta($post->ID,'_event_date_value',true)){回声 $ 日期.'<i class="fa fa-calendar"></i>',$date = get_post_meta($post->ID, '_event_date_value', true);回声'<br>';}如果(get_post_meta($post->ID,'_event_address_value',true)){回声$地址.'<i class="fa fa-map-marker"></i>',$address = get_post_meta($post->ID, '_event_address_value', true);}如果(get_post_meta($post->ID,'_event_city_value',true)){回声 $city.', ',$city = get_post_meta($post->ID, '_event_city_value', true);}?></section></节></文章><?php终了;万一;?></节>

<?php get_footer();?>

非常欢迎对 php 新手的任何建议.:)

解决方案

正如我已经说过的,您永远不应该使用 query_posts,因为它会破坏主查询和分页.如果您确实需要使用自定义查询,请使用 WP_Queryget_posts 进行自定义查询

从您的页面模板来看,我相信您正在使用页面循环获取自定义信息,然后使用自定义查询来显示您的活动帖子.

在我继续之前,专业提示,不要使用 :endifendwhile.尽管它是完全有效的 php,但由于代码编辑器不支持这种语法,因此很难调试.利用旧的忠实卷发.所有代码编辑器都支持它们,它们使调试更容易

这就是你的代码应该是什么样子:(坦率地说,我已经删除了标记和模板标签,所有这些代码从平板电脑发帖并不有趣)

//页面主循环,主查询如果 ( have_posts() ) {而 ( have_posts() ) {the_post();//你的标记和模板标签}}//在此处添加自定义即将发生的事件//现在让我们的循环显示事件帖子$args = 数组('post_type' = '事件','posts_per_page' =>30);$q = new WP_Query( $args );如果 ( $q->have_posts() ) {而 ( $q->have_posts() ) {$q->the_post();//您的自定义循环标记和模板标签}wp_reset_postdata();}

编辑

您完成的代码应如下所示:

<?php get_header();?><?php如果 (have_posts()) {而 (have_posts()) {the_post();if ( get_post_meta($post->ID, '_ct_hdr_value') ) { ?><div class="page-name innerpage<?php echo $post->post_name; ?>"><div class="row"><div class="twelvecol"><h1 class="page-h1"><?php the_title();?>

<?php } ?><div class="row"><div class="page-container"><div class="row"><div class="内容十二列"><?php the_content();?>

<?php}}?>

<section class="events cf"><h3 class="花式"><span>即将举行的活动</span><ul id="office-list" class="cf"><?php$args = 数组('post_type' = '事件','posts_per_page' =>30);$q = new WP_Query( $args );如果 ( $q->have_posts() ) {而 ( $q->have_posts() ) {$q->the_post();?><li class="sixcol cf"><article class="event cf"><a class="cf" href="<?php echo the_permalink(); ?>"><h5 class="text-center"><?php the_title();?></h5></a><br><a class="cf" href="<?php echo the_permalink(); ?>"><?php the_post_thumbnail('full', array('class' =>'img-center img-responsive event-thumb'));?></a><?php the_content() ?><section class="event-details"><section class="event-address cf"><?php$地址= $地址= $日期= $城市;如果(get_post_meta($post->ID,'_event_date_value',true)){回声 $ 日期.'<i class="fa fa-calendar"></i>',$date = get_post_meta($post->ID, '_event_date_value', true);回声'<br>';}if (get_post_meta($post->ID, '_event_address_value',true) ) {回声$地址.'<i class="fa fa-map-marker"></i>',$address = get_post_meta($post->ID, '_event_address_value', true);}如果(get_post_meta($post->ID,'_event_city_value',true)){回声 $city.', ',$city = get_post_meta($post->ID, '_event_city_value', true);}?></节></节></文章><?php}wp_reset_postdata();}?></节>

<?php get_footer();?>

I've created custom post type for events in WordPress and now I'm trying to list them all along with values from custom meta boxes on my page template that I called Events.

Values from meta boxes are being repeated and this is followed with a pattern:

This is a screenshot of what is going on:

This is a query that shows posts:

<?php 
/*
Template Name: Event
*/
?>
<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) :  the_post();
        if ( get_post_meta($post->ID, '_ct_hdr_value') ) : ?>

            <div class="page-name innerpage<?php echo $post->post_name; ?>">
                <div class="row">
                    <div class="twelvecol">            
                        <h1 class="page-h1"><?php the_title(); ?> </h1>

                    </div>
                </div>
            </div>
        <?php endif;?>
    <div class="row">
        <div class="page-container">
            <div class="row">
            <div class="content twelvecol">
                <?php echo the_content();
                endwhile; 
                endif; ?>
            </div>

        </div>
    <section class="events cf">
    <h3 class="fancy"><span>Upcoming events</span></h3>
    <ul id="office-list" class="cf">
    <?php
    query_posts(array('post_type' => 'event', 'posts_per_page' => 30) );
    if (have_posts()) : while (have_posts()) : the_post();?>
    <li class="sixcol cf">
    <article class="event cf">
    <a class="cf" href="<?php echo the_permalink(); ?>">
        <h5 class="text-center"><?php the_title(); ?></h5>
    </a>
    <br>
    <a class="cf" href="<?php echo the_permalink(); ?>">
    <?php the_post_thumbnail('full', array( 'class' => 'img-center img-responsive event-thumb')); ?>
    </a>
    <?php the_content() ?>
   <section class="event-details">
    <section class="event-address cf">   
    <?php
    $adress = $address = $date = $city;
            if (get_post_meta($post->ID, '_event_date_value',true) ) {
                echo $date. '<i class="fa fa-calendar"></i>  ',
                $date = get_post_meta($post->ID, '_event_date_value', true);
                echo '<br>';
            }
            if (get_post_meta($post->ID, '_event_address_value',true) ) {
                echo $address. '<i class="fa fa-map-marker"></i>  ',
                $address = get_post_meta($post->ID, '_event_address_value', true);
            }
            if (get_post_meta($post->ID, '_event_city_value',true) ) {
                echo $city. ', ',
                $city = get_post_meta($post->ID, '_event_city_value', true);
            }

    ?></section>
        </section>
    </article>
    </li>
    <?php
    endwhile;
    endif; ?>
</ul>
</section>
        </div>
    </div>
<?php get_footer(); ?>

Any advice for a php newbie is more than welcome. :)

解决方案

As I already stated, you should never use query_posts as it breaks the main query and pagination. Use WP_Query or get_posts for custom queries, if you really need the use of custom queries

From your page template, I believe you are using the page loop for custom info and then your custom query to show your event posts.

Just before I continue, pro tip, do not use : and endif and endwhile. Although it is perfectly valid php, it is hard to debug as code editors don't support this syntax. Make use of the old faithful curlies. All code editors support them, and they make debug much easier

This is what your code should look like: (I have removed the markup and template tags as frankly, posting from a tablet is not fun with all of that code)

// Page main loop, the main query
if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        // Your markup and template tags

    }
}

// Add you custom upcoming events heading here

// Now for our loop to show event posts
$args = array(
    'post_type' = 'event',
    'posts_per_page' => 30
);
$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

    // Your custom loop markup and template tags

    }
    wp_reset_postdata();
}

EDIT

Your completed code should look like this:

<?php 
/*
Template Name: Event
*/
?>
<?php get_header(); ?>

    <?php 
        if (have_posts()) {
            while (have_posts()) {
                the_post();
                if ( get_post_meta($post->ID, '_ct_hdr_value') ) { ?>

                    <div class="page-name innerpage<?php echo $post->post_name; ?>">
                        <div class="row">
                            <div class="twelvecol">            
                                <h1 class="page-h1"><?php the_title(); ?> </h1>
                            </div>
                        </div>
                    </div>
                <?php } ?>

                <div class="row">
                <div class="page-container">
                <div class="row">
                <div class="content twelvecol">
                    <?php the_content(); ?>
                </div>
                <?php
            }
        }
    ?>
            </div>


            </div>

    <section class="events cf">
        <h3 class="fancy">
            <span>Upcoming events</span>
        </h3>

        <ul id="office-list" class="cf">
            <?php
                $args = array(
                    'post_type' = 'event',
                    'posts_per_page' => 30
                );
                $q = new WP_Query( $args );

                if ( $q->have_posts() ) {
                    while ( $q->have_posts() ) {
                        $q->the_post(); ?>

                        <li class="sixcol cf">
                            <article class="event cf">

                                <a class="cf" href="<?php echo the_permalink(); ?>">
                                    <h5 class="text-center"><?php the_title(); ?></h5>
                                </a>

                                <br>

                                <a class="cf" href="<?php echo the_permalink(); ?>">
                                    <?php the_post_thumbnail('full', array( 'class' => 'img-center img-responsive event-thumb')); ?>
                                </a>

                                <?php the_content() ?>

                                <section class="event-details">
                                    <section class="event-address cf">   
                                        <?php
                                        $adress = $address = $date = $city;
                                        if (get_post_meta($post->ID, '_event_date_value',true) ) {
                                            echo $date. '<i class="fa fa-calendar"></i>  ',
                                            $date = get_post_meta($post->ID, '_event_date_value', true);
                                            echo '<br>';
                                        }
                                        if (get_post_meta($post->ID, '_event_address_value',true) ) {
                                            echo $address. '<i class="fa fa-map-marker"></i>  ',
                                            $address = get_post_meta($post->ID, '_event_address_value', true);
                                        }
                                        if (get_post_meta($post->ID, '_event_city_value',true) ) {
                                            echo $city. ', ',
                                            $city = get_post_meta($post->ID, '_event_city_value', true);
                                        }

                                        ?>
                                    </section>
                                </section>
                            </article>
                        </li>
                        <?php
                    }
                    wp_reset_postdata();
                }
            ?>
        </ul>
    </section>
    </div>
    </div>
<?php get_footer(); ?>

这篇关于来自自定义元框的值在帖子中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆