网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

如何在Wordpress的页面部分显示博客?

GG网络技术分享 2025-03-18 16:12 2


问题描述:

I\'m trying to display blog posts underneath the \'about us\' paragraph on an about page by using the code below in a template part. However, it\'s only returning the title of the actual page and the date info as the date I\'ve edited the page.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<article class=\"post\">

<header>

<h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>

<div class=\"post-details\">

<i class=\"fa fa-user\"></i><?php the_author_posts_link(); ?>

<i class=\"fa fa-calendar\"></i> <?php the_time( \'F jS, Y\' ); ?>

<i class=\"fa fa-folder-open-o\"></i> <a href=\"\"><?php the_category( \', \' ); ?></a>

<i class=\"fa fa-comments\"></i><a href=\"\"><?php comments_popup_link( \'No Comments »\', \'1 Comment »\', \'% Comments »\' ); ?></a>

</div><!-- post details -->

</header>

<div class=\"post-excerpt\">

<p><?php the_excerpt(); ?> <a href=\"post.html\">continue reading</a></p>

</div><!-- post-excerpt -->

<hr>

</article><!-- end article -->

<?php endwhile; else : ?>

<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>

<?php endif; ?>

What code do I need to pull my actual blog posts into this section?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在尝试使用下面的代码在about页面上的\'about us\'段下面显示博客帖子 在模板部分。 但是,它只返回实际页面的标题和日期信息作为我编辑页面的日期。</ p>

 &lt;?php if(have_posts()):  while(have_posts()):the_post();  ?&gt; 

&lt; article class =“post”&gt;

&lt; header&gt;

&lt; h3&gt;&lt; a href =“&lt;?php the_permalink();?&gt;”&gt;&lt;? php the_title(); ?&gt;&lt; / a&gt;&lt; / h3&gt;

&lt; div class =“post-details”&gt;

&lt; i class =“fa fa-user”&gt;&lt; / i&gt;&lt;? php the_author_posts_link(); ?&gt;

&lt; i class =“fa fa-calendar”&gt;&lt; / i&gt; &lt;?php the_time(\'F jS,Y\'); ?&gt;

&lt; i class =“fa fa-folder-open-o”&gt;&lt; / i&gt; &lt; a href =“”&gt;&lt;?php the_category(\',\'); ?&gt;&lt; / a&gt;

&lt; i class =“fa fa-comments”&gt;&lt; / i&gt;&lt; a href =“”&gt;&lt;?php comments_popup_link(\'No Comments»\',\' 1条评论»\',\'%评论»\'); ?&gt;&lt; / a&gt;

&lt; / div&gt;&lt;! - 发布详细信息 - &gt;

&lt; / header&gt;

&lt; div class =“post-excerpt”&gt ;

&lt; p&gt;&lt;?php the_excerpt(); ?&GT; &lt; a href =“post.html”&gt;继续阅读&lt; / a&gt;&lt; / p&gt;

&lt; / div&gt;&lt;! - - 摘录后 - &gt;

&lt; hr&gt;

&lt; / article&gt;&lt;! - 最终文章 - &gt;

&lt;?php endwhile; 否则:?&gt;

&lt; p&gt;&lt;?php _e(\'抱歉,没有符合条件的帖子。\'); ?&gt;&lt; / p&gt;

&lt;?php endif; ?&gt;

</ code> </ pre>

我需要哪些代码才能将我的实际博客文章纳入此部分? </ p>

</ div>

网友观点:

In your snippet the custom query for your posts is missing. Try something like this:

    // WP_Query arguments

$args = array(

\'post_type\' => \'post\',

\'post_status\' => \'publish\'

);

$custom_query = new WP_Query( $args );

<?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

<article class=\\\"post\\\">

<header>

<h3><a href=\\\"<?php the_permalink(); ?>\\\"><?php the_title(); ?></a></h3>

<div class=\\\"post-details\\\">

<i class=\\\"fa fa-user\\\"></i><?php the_author_posts_link(); ?>

<i class=\\\"fa fa-calendar\\\"></i> <?php the_time( \'F jS, Y\' ); ?>

<i class=\\\"fa fa-folder-open-o\\\"></i> <a href=\\\"\\\"><?php the_category( \', \' ); ?></a>

<i class=\\\"fa fa-comments\\\"></i><a href=\\\"\\\"><?php comments_popup_link( \'No Comments »\', \'1 Comment »\', \'% Comments »\' ); ?></a>

</div><!-- post details -->

</header>

<div class=\\\"post-excerpt\\\">

<p><?php the_excerpt(); ?> <a href=\\\"post.html\\\">continue reading</a></p>

</div><!-- post-excerpt -->

<hr>

</article><!-- end article -->

<?php endwhile; else : ?>

<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>

<?php

// Restore original Post Data

wp_reset_postdata();

endif;

?>

Here you can find an useful tool to generate a Wordpress Query:

https://generatewp.com/wp_query/

Here you can find permitted arguments for Wordpress Query:

https://developer.wordpress.org/reference/classes/wp_query/

To use your custom query remember to call have_posts() and the_posts() methods with your query object ( $custom_query->have_posts() and $custom_query->the_post() in snippet), furthermore is important wp_reset_postdata() to restore main query.

标签:

提交需求或反馈

Demand feedback