WordPress动态自定义菜单没有显示正确的结果 [英] WordPress dynamic custom menu not showing correct results

查看:137
本文介绍了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参数
$ args = array(
'category_name'=>'cat-html',
'order'=>'ASC',
'orderby'=>'date',
);

//查询
$ 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()。>._title()。< / a>< / li>; * /

$ query-> the_post();


$ b / * echo< / div>; * /
}

//恢复原始发布数据
wp_reset_postdata();
}
}?>
<! - ##2015年12月12日星期六由Aryansh Malviya(ARVIS APPS)添加
#为特定任务添加自定义菜单
//开始 - >
<?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>

这段代码并没有做我认为应该做的事情,下面是一张图片, :

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

解决方案

您可以更好地执行此操作: ,创建一个名为 sidebar-custom1.php 的文件并复制此代码:

 < div class =col-md-4 enigma-sidebar> 
< div class =enigma_sidebar_widget>
< div class =enigma_sidebar_widget_title>
< h2>侧栏标题< / h2>
<?php getPostsByCategoryID(4); ?>
< / div>
< / div>
< / div>

然后,在要调用边栏的页面中,粘贴以下代码:

 <?php get_sidebar('custom1'); ?> 

这将包含所需的侧边栏。


I am creating a dynamic custom menu that shows all posts links, like the menu widget in the sidebar, of a certain category. It is supposed to be dynamic, meaning that whenever I make a post in that category, the menu should include the post that I made without me having to physically drag & drop a new post in the menu.

Here's my code: (the category ID of which's posts I want : 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>

This code is not doing what I think it supposedly should do, here's a picture showing what this results into:

I am not familiar with either WordPress or PHP so please forgive any silly mistakes.

解决方案

You can better do this:

In your theme map, create a file named sidebar-custom1.php and copy this code:

<div class="col-md-4 enigma-sidebar">
    <div class="enigma_sidebar_widget">
        <div class="enigma_sidebar_widget_title">
            <h2>Sidebar title</h2>
            <?php getPostsByCategoryID(4); ?>
        </div>
    </div>
</div>

Then, in the page you want to call the sidebar, you paste this code:

<?php get_sidebar('custom1'); ?>

That will include the sidebar wherever you want it.

这篇关于WordPress动态自定义菜单没有显示正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆