Skip to content Skip to sidebar Skip to footer

Wordpress Dynamic Custom Menu Not Showing Correct Results

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 ma

Solution 1:

In functions.php add this function:

functiongetPostsByCategoryID($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 ( $allpostsas$p ):
        echo'<li><a href="'. get_permalink($p->ID) . '">' . get_the_title($p->ID) . '</a></li>';
    endforeach;
}

Use it like this for your sidebar or wherever you want:

<?php getPostsByCategoryID(HERE_THE_CATEGORY_ID); ?>

For example:

<?php getPostsByCategoryID(4); ?>

Solution 2:

You can better do this:

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

<divclass="col-md-4 enigma-sidebar"><divclass="enigma_sidebar_widget"><divclass="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.

Post a Comment for "Wordpress Dynamic Custom Menu Not Showing Correct Results"