最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Select pages by category

programmeradmin0浏览0评论

Is it possible to select pages based on category with the function get_pages? I've already added cats to my page taxonomy. The code below simply pulls all of my pages ignoring $args:

<?php

    echo '<ul>';
    $args = array(
        'category' => 25
    ); 
    $pages = get_pages($args);
    foreach ( $pages as $page ) {
        $option = '<li><a href="' . get_page_link( $page->ID ) . '">';
        $option .= $page->post_title;
        $option .= '</a></li>';
        echo $option;
     }
    echo '</ul>';

?>

Is it possible to select pages based on category with the function get_pages? I've already added cats to my page taxonomy. The code below simply pulls all of my pages ignoring $args:

<?php

    echo '<ul>';
    $args = array(
        'category' => 25
    ); 
    $pages = get_pages($args);
    foreach ( $pages as $page ) {
        $option = '<li><a href="' . get_page_link( $page->ID ) . '">';
        $option .= $page->post_title;
        $option .= '</a></li>';
        echo $option;
     }
    echo '</ul>';

?>
Share Improve this question asked Sep 16, 2016 at 20:22 brandozzbrandozz 8121 gold badge14 silver badges27 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I was able to work this out thanks to the following post How to Add Categories to Pages in Wordpress. I needed to modify WP_query to include pages when displaying a list of posts. Below is the solution:

In my template file:

$args = array(
        'category'  => 25,
        'orderby'   => 'date',
        'order'     => 'ASC'
    ); 
    $pages = get_posts($args);
    foreach ( $pages as $page ) {
        setup_postdata($page);
        $mykey_values = get_post_custom_values( 'webinar_link', $page->ID );
        echo '<div class="media webinar webinar-upcoming">';
        echo '<a class="pull-left" href="' . get_page_link($page->ID) . '" title="' . $page->post_title . '">';
        if ( has_post_thumbnail( $page->ID ) ) {
            $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'optional-size' );
            $image = $image_array[0];
        }
        echo '<img class="media-object responsive-image" src="' . $image . '"></a>';
        echo '<div class="media-body extra-bottom-pad">';
        echo '<h2><a href="' . get_page_link( $page->ID ) . '">' . $page->post_title . '</a></h2>';
        the_content('');
        echo '<a class="pull-left vistex-button-sm more-link" href="' . get_page_link($page->ID) . '" title="' . $page->post_title . '">Learn More</a>';
        foreach ($mykey_values as $key => $value) {
            echo '<p><a class="vistex-button-sm more-link" style="margin-left: 10px;" target="_blank" href="' . $value . '">Register Now</a></p>'; 
        }
        echo '</div>';
        echo '</div>';

And in my functions file:

/* add categories to pages */
function cats_pages() {  
    // Add category metabox to page
    register_taxonomy_for_object_type('category', 'page');  
}
add_action( 'init', 'cats_pages' );

function add_taxonomies_to_pages() {
     register_taxonomy_for_object_type( 'post_tag', 'page' );
     register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_taxonomies_to_pages' );

if (!is_admin() ) {
    add_action( 'pre_get_posts', 'category_and_tag_archives' );
}

function category_and_tag_archives( $wp_query ) {
    $my_post_array = array('post','page');

    if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
     $wp_query->set( 'post_type', $my_post_array );

     if ( $wp_query->get( 'tag' ) )
     $wp_query->set( 'post_type', $my_post_array );

This did add every single page in my site to the blog so you need to then select the categories that you want to list.

In my index.php file:

<?php $query = new WP_Query('cat=-7,-19,17,18,9,15,16,1'); ?>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论