php – WordPress动态自定义菜单无法显示正确的结果

我正在创建一个动态自定义菜单,显示所有帖子链接,如边栏中的菜单小部件,某个类别.它应该是动态的,这意味着无论何时我在该类别中发帖,菜单都应该包含我制作的帖子,而不必在物理上拖动和放置.在菜单删除一个新帖子.

这是我的代码:(我想要的帖子的类别ID:4)

<div class="col-md-4 enigma-sidebar">
    <?PHP if ( is_active_sidebar( 'sidebar-primary' ) )
    { dynamic_sidebar( 'sidebar-primary' ); }
    else  { 
    $args = array(
    'before_widget' => '<div class="enigma_sidebar_widget">',
    'after_widget'  => '</div>',
    'before_title'  => '<div class="enigma_sidebar_widget_title"><h2>',
    'after_title'   => '</h2></div>' );
    the_widget('WP_Widget_Archives', null, $args);
    } ?>

<?PHP  /*Menu LooP*/
function menu1_loop() {

global $post;

$args = array(
    'type'                     => 'post',
    'orderby'                  => 'date',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'include'                  => '4',
    'number'                   => '',
    'taxonomy'                 => 'category',

); 

$categories = get_categories( $args );
foreach($categories as $category) {

// WP_Query arguments
$args = array (
    'category_name'          => 'cat-html',
    'order'                  => 'ASC',
    'orderby'                => 'date',
);

// The Query
$query = new WP_Query( $args );

//Loop
if ( $query->have_posts() ) {
  /*echo "<div>"; */
    while ( $query->have_posts() ) {

      $post.the_permalink();
      $post.the_title();
      /*echo "<li><a href=".the_permalink().">".the_title()."</a></li>";*/

        $query->the_post();

         }

  /*echo "</div>";*/
    }

    // Restore Original post data
    wp_reset_postdata();
} 
} ?>
  <!-- # Added by Aryansh Malviya(ARVIS APPS) on Saturday, December 12th, 2015 
  # Added to make a custom menu for specific task
  // begins -->
  <?PHP wp_nav_menu( array( 'theme_location' => 'html-menu', 'container_class' => 'enigma_sidebar_widget'  ) /*.menu1_loop()*/ ); ?>
    <?PHP wp_nav_menu( array( 'theme_location' => 'PHP-menu', 'container_class' => 'enigma_sidebar_widget' ) ); ?>
  <!-- // ends -->

</div>

这段代码没有做我认为应该做的事情,这里有一张图片显示了这个结果:

Menu Problem ARVIS APPS

我不熟悉wordpressPHP,所以请原谅任何愚蠢的错误.

解决方法:

在functions.PHP添加函数

function getPostsByCategoryID($categoryID)
{
    $args = array(
    'posts_per_page'   => -1,
    'offset'           => 0,
    'category'         => $categoryID,
    'orderby'          => 'date',
    'order'            => 'ASC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    );    

    $allposts = get_posts( $args );
    foreach ( $allposts as $p ):
        echo '<li><a href="'. get_permalink($p->ID) . '">' . get_the_title($p->ID) . '</a></li>';
    endforeach;
}

像这样在你的侧边栏或任何你想要的地方使用它:

<?PHP getPostsByCategoryID(HERE_THE_CATEGORY_ID); ?>

例如:

<?PHP getPostsByCategoryID(4); ?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...