【问题标题】:how to customize post in wordpress?如何在wordpress中自定义帖子?
【发布时间】:2017-05-01 20:33:34
【问题描述】:

我尝试使用 wordpress 中的简码方式自定义我自己的帖子。我想根据帖子的类别显示帖子。

<?php

function sample_post($atts) {
   extract(shortcode_atts(array(
      'category_name' => 'uncategorized'
   ), $atts));

  $args = array('category_name' => $category_name);
   query_posts($args);
   if (have_posts()) : while (have_posts()) : the_post(); ?>
         <label><?php the_title(); ?></label>
   <?php
        endwhile;
   endif;
}

add_shortcode('sample_post', 'sample_post');

?>

代码很好,我的模板名称中有一个文件content-page.php 这是我的内容页面的代码

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <h1 class="entry-title"><?php the_title(); ?></h1>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'unite' ),
                'after'  => '</div>',
            ) );

            edit_post_link( __( 'Edit', 'foodgrower' ), '<span class="edit-link">', '</span>' );
        ?>
    </div><!-- .entry-content -->

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

我不知道我是否错过了有关设置或查询的内容。这是我真正想要展示的。我只想显示&lt;label&gt;&lt;?php the_title(); ?&gt;&lt;/label&gt;(帖子的标题),但现在显示的是content-page.php。我也不明白为什么它会出现在我的页面中。我没有调用 content-page.php 来显示在我的页面中。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    更改代码以返回数据,如下所示,

    function sample_post($atts) {
       $return = '';
       extract(shortcode_atts(array(
          'category_name' => 'uncategorized'
       ), $atts));
    
      $args = array('category_name' => $category_name);
       $the_query = new WP_Query( $args );
       if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
             $return .= "<label>".the_title()."</label>";
            endwhile;
       endif;
       return $return;
    }
    
    add_shortcode('sample_post', 'sample_post');
    

    【讨论】:

    • 仍然 content_page.php 工作。结果还是一样
    • 问题看起来不同。尝试使用 WP_Query() 将其关闭。我可以举个例子吗?
    • 你能提供样品吗
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多