如何在我的wordpress主题页面上显示特定帖子

时间:2016-07-07 20:28:30

标签: php wordpress

我正在尝试在我的wordpress主题上显示页面上的特定帖子。在这种情况下,我试图在主页上显示帖子,我已经尝试了很多,至少显示标题,但我得到的只是页面的标题,而不是帖子本身。

我已经尝试the_titleget_the_title(),但魔法没有发生。

Example

以下是与我的问题相关的代码。

home.php

<?php

    if ( have_posts() ) :


        /* Start the Loop */
        while ( have_posts() ) : the_post();?>


    <?php
            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            // if ( has_post_format( 'video' )) {
            //   echo 'this is the video format';
            // }
            get_template_part( 'template-parts/content-chat', get_post_format('chat') );

        endwhile;


    else :

        // get_template_part( 'template-parts/content', 'none' );

    endif; ?>

它调用文件(调用正确的文件,双重检查)

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
    <?php the_content(); 
        echo get_post_format();
    ?>


</article><!-- #post-## -->

总之,问题是:

1)为什么显示页面标题?
2)如何在我的页面中正确显示这种帖子格式?

1 个答案:

答案 0 :(得分:0)

对于你的第一个问题,wp基于query_posts()抓取主页的标题,这就是使wordpress如此动态的原因,你可以实际检查页面或类别ID,然后选择查询帖子上显示的内容。由于您的代码中没有query_posts,因此wp默认为当前帖子ID,即您的主页。

对于你的第二个,我有一个代码片段,但它在家里,你可以在这里查找 https://developer.wordpress.org/reference/functions/query_posts/

好的,所以这里是片段。对不起,我一直在度假。

首先,您必须确定主页的ID,或者您可以使用is home page功能。如果你走的是前一条路线,你只需转到主页的后端,然后用鼠标悬停在它上面。主页应显示在页面底部的链接工具提示中。这将是post = 5,这个数字就是你所需要的。 接下来,我将为您要在首页上显示的帖子创建一个类别,并从后端的类别页面中获取该ID。再次,它将像tag_id = 12

 <?php 

            $currpage = $post -> ID;
            if($currpage == 5) { 
              query_posts('showposts=1&cat=12');
            }
            while (have_posts()): the_post();  

您可以通过这种方式在一个页面上拥有多个帖子,事实上您可以在该页面上拥有整个循环,然后在其下面动态填充其下的其他帖子,如下例所示: http://testex-ndt.com/products/ect-products/顶部是工作循环的页面,底部(推荐)是分配给特定类别的帖子,此处的showposts设置为3,将显示最新三个帖子的摘录。这是我过去做过的一个网站。

您可能需要记住的另一件事是页面模板,如果您希望一个页面以一种方式工作而您希望另一种类型的页面工作另一个页面,则必须考虑模板化。

这是该页面的完整代码。

<?php
/*
Template Name:Main Product
*/
?>
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
    <?php if (have_posts()) : ?>
        <?php while (have_posts()): the_post(); ?>
        <?php $postName = get_the_title($post); ?>
    <!-- title link dynamic -->
        <?php edit_post_link('Edit this item','',' | '); ?>
        <?php the_content('Continue Reading'); ?>
        <?php posts_nav_link(); ?>
        <?php endwhile; ?>
        <?php else : ?>
        <div class="w3-col text">
        <h2>Not Found</h2>
        <p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
        <?php get_search_form(); ?>
        </div>
        <?php endif; ?>

    <!-- insert testimonial here -->
    <section class="w3-row cl testimonials">


    <!-- make code for query based on the page, then get the top three testimonials in excerpt form  This is commented out because I did it with a function I attached to the header I will show below
        $currpage = $post -> ID;
        if($currpage == xx){
            $cat  = x;
        }
            -->

        <h2>Testimonials for <?php echo $postName; ?></h2>  
            <?php 
            $currpage = $post -> ID;
            $cat = testimonial($currpage);  //here is the function 
            query_posts('showposts=3&cat=' .$cat ); ?>
        <?php while (have_posts()): the_post(); ?>
        <div class="w3-col excerpt" style="width:30%;"> 
        <?php the_excerpt(''); ?>
            </div>
        <?php endwhile;
         wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS

      ?>
    </section>


    <!-- /content float -->
    </div>
<!-- /content -->
</div>

所以函数设置就像一个单独的php文件,所以我可以模块化地改变它。我调用了category_help.php文件

<?php
/*This particular block of code is only for determining what category     of testimonial is allowed in a post. */

function testimonial($myPageID){
    //services (all cats)
    $id = "-15,-14,-13";
    //Products 
    //lfet
    if($myPageID == 18) $id = 2;
    //bfet
    if($myPageID == 22) $id = 4;
    //rfet
    if($myPageID == 20) $id = 3;
    //ect
    if($myPageID == 24) $id = 5;
    //ultrasound
    if($myPageID == 26) $id = 8;
    return $id;
} 
?>

然后我在header.php

的顶部调用了它
<?php require 'category_help.php'; ?>
相关问题