【问题标题】:Wordpress echo the_title in loop issueWordpress 在循环问题中回显 the_title
【发布时间】:2011-04-02 12:36:10
【问题描述】:

无法解决此问题。我已经为我的 wordpress 主题设置了一个自定义模板,并试图将当前存在类别链接和标题回声的位更改为帖子标题的回声。

目前的回声是:

echo '<h2><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h2>';

有人可以帮忙吗?

整页代码为:

<div id="older-posts">
<?php
// Get the current category
foreach((get_the_category()) as $category) 
{ 
    $current_cat_id = $category->cat_ID; 
    break;
}

// Set the category to only the category selected
$args = array(
    'category__in' => array($current_cat_id),
    'orderby' => 'date',
    'order' => 'DESC'
);

$posts = new WP_Query();
$posts->query($args);

if ($posts->have_posts())
{
    while ($posts->have_posts())
    {
        echo '<div class="result">';
        echo '<h2><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h2>';
        $posts->the_post();
        ?> 
        <div class="thumbnail">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
                    <h3><?php the_title(); ?></h3>
                    <?php //get thumnbnail (custom field) ?>
                    <?php $image = get_post_meta($post->ID, 'thumbnail', true); ?>
                    <img src="<?php echo $image; ?>" title="<?php the_title(); ?>" />
                    <?php the_post_thumbnail(); ?>
                </a>
        </div>  </div>
        <?php 

    }   
}

// Reset global query
wp_reset_query();
?>

</div>

【问题讨论】:

    标签: wordpress echo


    【解决方案1】:

    我不明白你的意思。但是试试这个:

    //just after while() start
    $posts->the_post();
    ?> 
    <div class="result">
        <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <div class="thumbnail">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
                <h3><?php the_title(); ?></h3>
                <?php //get thumnbnail (custom field) ?>
                <?php $image = get_post_meta($post->ID, 'thumbnail', true); ?>
                <img src="<?php echo $image; ?>" title="<?php the_title(); ?>" />
                <?php the_post_thumbnail(); ?>
            </a>
        </div>
    </div>
    

    【讨论】:

    • 完美。正如您建议的那样,在 $posts->the_post(); 之后移动帖子标题和链接;成功了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多