doufenzhu7012 2016-07-07 20:28
浏览 31
已采纳

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

I'm trying to show on my wordpress theme a specific post on a page. In this case, I'm trying to show the post on the home page and I've tried a lot to at least show the title, but all that I get is the title of the page, not of the post itself.

I've tried the_title and get_the_title() but the magic didn't happen.

Example

Here comes the code relevant to my problem.

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

And it calls the file (it is calling the right file, double checked)

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

In summary, the questions are:

1) Why is it showing the title of the page?
2) How can I show properly this kind of post format in my page?

  • 写回答

1条回答 默认 最新

  • dongxing8009 2016-07-07 20:32
    关注

    For your first question wp grabs the title of the home page based on query_posts() this is what makes wordpress so dynamic, you can actually check a page or category id and then select with query posts what to show on it. Since you do not have a query_posts in your code wp defaults to the current post id which is your home page.

    For your second, I had a code snippet, but it is at home, you can look it up here https://developer.wordpress.org/reference/functions/query_posts/

    Ok, so here is the snippet. Sorry for the delay, I have been on vacation.

    First you have to determine the ID of your home page, or you can use the is home page function. If you go the former route you simply go to the back end to your home page and hover over it with your mouse. the home page should show up in a link tooltip at the bottom of your page. It will be something like post=5 the number is what you need. Next I would create a category for the post you want to display on the front page and get that ID from the category page in the back end. Again it will be something like tag_id=12

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

    You can have multiple posts on one page this way, infact you can have your whole loop on that page then under it dynamically populate other posts under it like in this example: http://testex-ndt.com/products/ect-products/ The top part is the page that is working a loop and the bottom (testimonials) are posts that are assigned to a specific category, the showposts here are set to 3 and will display excerpts of the latest three posts. This is one website I did in the past.

    One other thing you may want to keep in mind is page templates, if you want one page to work one way and you want another type of page to work another you will have to look into templating.

    here is the entire code for that page.

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

    So the Function was set up like as a seperate php file so I could alter it modularly. I called the file 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;
    } 
    ?>
    

    Then I called this in the top of header.php

    <?php require 'category_help.php'; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf通过绑定控件自身的值,来实现背景颜色的切换
  • ¥15 CDH6.3 运行hive -e hive -e "show databases;"报错:hive-env.sh:行24: hbase-common.jar: 权限不够
  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线