WordPress动态自定义菜单未显示正确结果

412*_*157 5 html 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>
Run Code Online (Sandbox Code Playgroud)

这段代码没有执行我认为应该执行的操作,下面的图片显示了结果: 菜单问题ARVIS APPS

我不熟悉WordPress或PHP,因此请原谅任何愚蠢的错误。

小智 0

在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;
}
Run Code Online (Sandbox Code Playgroud)

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

<?php getPostsByCategoryID(HERE_THE_CATEGORY_ID); ?>
Run Code Online (Sandbox Code Playgroud)

例如:

<?php getPostsByCategoryID(4); ?>
Run Code Online (Sandbox Code Playgroud)