Fernando Souza
Fernando Souza

Reputation: 1779

List WordPress certain category's post url

Is there a way to list all the posts urls from a certain category in WordPress, without a plugin? I am not familiar with PHP, but I was thinking if some how, I could use a page template where a method would call all this category posts url(ex:lets call the category "blog").

Upvotes: 0

Views: 79

Answers (2)

giolliano sulit
giolliano sulit

Reputation: 1006

You can use get_posts (https://codex.wordpress.org/Template_Tags/get_posts).

Consider this a slimmed down version of WP_Query which will return an array of the posts you want.

$categoryPosts = get_posts(array(
    // Note: The category parameter needs to be the ID of the category, and not the category name.
    'category' => 1,
    // Note: The category_name parameter needs to be a string, in this case, the category name.
    'category_name' => 'Category Name',
));

Then you can loop through the posts:

foreach($categoryPosts as $categoryPost) {
    // Your logic
}

$categoryPost will contain the following by default (more fields if you have custom fields), these fields will obviously be populated, but this is what you'll have available in an array:

WP_Post Object
(
    [ID] =>
    [post_author] => 
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => 
    [comment_status] => 
    [ping_status] => 
    [post_password] => 
    [post_name] => 
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] => 
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] => 
    [post_type] => 
    [post_mime_type] => 
    [comment_count] => 
    [filter] =>
)

Upvotes: 1

git-e-up
git-e-up

Reputation: 904

Review WP_Query and the WordPress loop. Something like this ought to work if I'm understanding your inquiry correctly (wrap in php tags):

$the_query = new WP_Query( array( 'category_name' => 'blog' ) );

if ( $the_query->have_posts() ) {
 echo '<ul>';
 while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_permalink() . '</li>';
 }
 echo '</ul>';
 /* Restore original Post Data */
 wp_reset_postdata();
} else {
 // no posts found
} 

Upvotes: 1

Related Questions