首页 文章

在the_content过滤器中显示自定义帖子类型帖子和元数据

提问于
浏览
1

我在mu-plugins.php文件中使用它//

function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>
<figure>
   <?php the_post_thumbnail('thummy'); ?>
   <figcaption>
<h3><?php the_title(); ?></h3>
<span>Cool stuff brah.</span>

<?php 
 if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
                    </figcaption>
                </figure>
            </li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');

在我的页面模板中,我使用 <?php the_content(); ?> 来显示所有内容 .

UPDATE//
完整页面模板代码

<div id="container" class="clearfix">
<div id="left-content">
    <?php get_sidebar('two');?> 
</div>

<div id="right-content">
    <h1><?php wp_title(''); ?></h1>

    <ul class="grid cs-style-3">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
    <?php endwhile; ?>
    <?php endif; ?>
    </ul>
</div><!--right-content-->
</div><!--container-->

<?php get_footer(); ?>

但是我收到了这个错误//
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/domain.com/testing/wp-content/mu-plugins/must-use.php on line 15

我正在尝试这种显示内容的方法,因为我想使用WP-Members插件,我意识到该插件只适用于 the_content() 内的内容 .

所以我的问题是如何修复上面发布的代码以正确的方式显示 Headers ,缩略图,链接等?

2 回答

  • 0
    function new_default_content($content) {
    global $post;
    if ($post->post_type == 'textures') {
    $content .='<li>';
    $content .='<figure>';
    $content .= the_post_thumbnail('thummy');
    $content .= '<figcaption>';
    $content .= '<h3>';
    $content .= the_title();
    $content .= '</h3>';
    $content .= '<span>Cool stuff brah.</span>';
    
     if ( has_post_thumbnail()) {
        $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
        echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; 
     }
            $content .= '</figcaption>';
        $content .= '</figure>';
    $content .= '</li>';
    }
    return $content;
    }
    add_filter('the_content', 'new_default_content');
    

    试试这个代码 . 如果您仍然得到相同的错误,我们将检查 .

  • 2

    FOUND A SOLUTION
    我记得你可以在短代码中添加内容 .
    所以我创建了自己的短代码 [textures_content]

    然后使用下面粘贴的代码在 <?php the_content(); ?> 函数中显示内容//

    add_shortcode( 'textures_content', 'textures_shortcode' );
    function textures_shortcode( $atts ) {
        ob_start();
        $query = new WP_Query( array(
            'post_type' => 'textures',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'orderby' => 'date',
        ) );
        if ( $query->have_posts() ) { ?>
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <li>
                        <figure>
                            <?php the_post_thumbnail('thummy'); ?>
                            <figcaption>
                                <h3><?php the_title(); ?></h3>
                                <?php if ( has_post_thumbnail()) {
                                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
                                echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
                            </figcaption>
                        </figure>
                    </li>
                <?php endwhile;
                wp_reset_postdata(); ?>
        <?php $myvariable = ob_get_clean();
        return $myvariable;
        }
    }
    

    现在,仅会员插件按预期工作,登录后显示被阻止的内容:)

相关问题