Wordpress echo the_title 循环问题

发布于 2024-10-29 14:02:36 字数 1684 浏览 3 评论 0原文

无法为此制定解决方案。我已经为我的 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>

Unable to work out a solution to this. I have set up a custom template for my wordpress theme and am trying to change the bit where there is currently an echo of the category link and title to an echo of the title of the post.

Currently the echo is:

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

Can anyone help please?

Full page code is:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-11-05 14:02:36

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

//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>

I don't get your point. But try this:

//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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文