首页 文章

Wordpress php无法在每个帖子的帖子列表页面上显示自定义元信息

提问于
浏览
0

我在WordPress中创建了一个自定义帖子类型,对于该自定义帖子类型,我有自定义元框,我可以在这里输入每天或每周产品租赁的价格(有2个元框) .

我想在显示自定义帖子类型的所有帖子的页面上显示这些价格 . 我的主题在帖子的 Headers 之后有一个动作挂钩,这是我想要显示元信息的地方 .

我已设法挂钩动作,但是,我的信息仅在第一篇文章中显示,而不是在所有帖子上显示 .

这是我用来回显元信息的代码:

function add_prices_to_products() {
    global $post;
    $price_per_day = get_post_meta($post->ID, "_per_day", true); 
    $price_per_week = get_post_meta($post->ID, "_per_week", true); 
    echo '<span class="sl_product_price per_day">£' . $price_per_day . '/day</span>';   
    echo '<span class="sl_product_price per_week">£' . $price_per_week . '/wk</span>';   
}
add_action('layers_after_list_post_title', 'add_prices_to_products');

任何人都可以告诉我为什么它只是添加到第一篇文章而不是所有这些(我可以确认元信息正确保存)?

这是主题(index.php)的页面代码:

<?php get_header(); ?>

<div class="container content-main archive clearfix">
<?php get_sidebar( 'left' ); ?>

<?php if( have_posts() ) : ?>
    <div <?php layers_center_column_class(); ?>>
        <?php while( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'partials/content' , 'list' ); ?>
        <?php endwhile; // while has_post(); ?>
        <?php the_posts_pagination(); ?>
    </div>
<?php endif; // if has_post() ?>

<?php get_sidebar( 'right' ); ?>
</div>
<?php get_footer();

它引用了包含钩子的文件content-list.php .

global $post, $layers_post_meta_to_display; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'push-bottom-large' ); ?>>
<?php do_action('layers_before_list_post_title'); ?>
<header class="section-title large">
    <?php do_action('layers_before_list_title'); ?>
    <h1 class="heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php do_action('layers_after_list_title'); ?>
</header>
<?php do_action('layers_after_list_post_title'); ?>

<?php /**
* Display the Featured Thumbnail
*/
echo layers_post_featured_media( array( 'postid' => get_the_ID(), 'wrap_class' => 'thumbnail push-bottom', 'size' => 'large' ) ); ?>

<?php if( '' != get_the_excerpt() || '' != get_the_content() ) { ?>
    <?php do_action('layers_before_list_post_content'); ?>
    <?php do_action('layers_list_post_content'); ?>
    <?php do_action('layers_after_list_post_content'); ?>
<?php } ?>

<?php do_action('layers_before_list_post_meta'); ?>
    <?php /**
    * Display the Post Meta
    */
    layers_post_meta( get_the_ID(), NULL, 'footer', 'meta-info push-bottom' ); ?>
<?php do_action('layers_after_list_post_meta'); ?>

<?php do_action('layers_before_list_read_more'); ?>
<?php do_action('layers_list_read_more'); ?>
<?php do_action('layers_after_list_read_more'); ?>
</article>

值得一提的是,我正在插件中插入

UPDATE

使用钩子“the_post”我能够在列表中的每个帖子上回显纯文本,但是,我无法得到我的元信息信息 . 这是我现在使用的:

add_action( 'the_post', 'my_custom_loop_start' );

function my_custom_loop_start( $query )
{
echo 'hello';
    add_action( 'loop_end', 'my_custom_loop_end' );
}

function my_custom_loop_end()
{
remove_action( 'layers_after_list_post_title', 'add_prices_to_products' );   
}

1 回答

  • 0

    你需要挂钩循环中的每个项目,尝试这样的事情......

    // Hook into the main loop    
    add_action( 'loop_start', 'my_custom_loop_start' );
    
        // Add two filters in that loop - your existing function and one to clear it out
        function my_custom_loop_start( $query )
        {
            if( $query->is_main_query() )
            {
                add_filter( 'layers_after_list_post_title', 'add_prices_to_products' );
                add_action( 'loop_end', 'my_custom_loop_end' );
            }
        }
    
        // This will just remove your action once each loop is done so you get the correct data for each loop
        function my_custom_loop_end()
        {
            remove_action( 'layers_after_list_post_title', 'add_prices_to_products' );   
        }
    

    您可能需要调整它才能正常工作,但逻辑是将您的函数添加到每个循环,运行它,然后删除它 .

相关问题