Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show custom fields on category template WordPress

  • SOLVED

Hi,

I have the following categories (slug):

portfolio
- publicite
- corporatif
- etc..

The categories don't have a particular template assigned, so they use category.php

I also have the following pages:

Portfolio
- Publicité
- Corporatif
- etc..

Each of these pages have 3 custom fields (key, value): (slider1, url/to/image.jpg) and I want to output these values on the category template.

I got something working for the page.php template but it doesn't work on the category.php template.

Here is my working code:


<?php if ( is_page('name-of-page') ) :
// Show a slider on the Name-Of-Page page

global $wp_query;
$postid = $wp_query->post->ID; ?>

<?php echo get_post_meta($postid, 'slider1', true); ?>
<?php echo get_post_meta($postid, 'slider2', true); ?>
<?php echo get_post_meta($postid, 'slider3', true); ?>

<?php wp_reset_query(); ?>
<?php endif; ?>


But this <strong>doesn't</strong> work:


<?php if ( is_category() ) :
// Show a slider on the category page

global $wp_query;
$postid = $wp_query->post->ID; ?>

<?php echo get_post_meta($postid, 'slider1', true); ?>
<?php echo get_post_meta($postid, 'slider2', true); ?>
<?php echo get_post_meta($postid, 'slider3', true); ?>

<?php wp_reset_query(); ?>
<?php endif; ?>

Answers (6)

2011-08-13

Daniele Raimondi answers:

If I got it right you'd like to put the same slideshow on category "publicite" and on page "publicite" and so on.

If your pages share the same slug of the relative category, try this code:


<?php
if ( is_category() ) :
//get this category object
$this_cat=$wp_query->get_queried_object();

//get the page object whit the same slug
$relative_page= get_post_by_slug( array( 'posttype'=>'page', 'slug'=>$this_cat->slug ) );
?>

<?php // Show a slider on the category page
echo get_post_meta($relative_page->ID, 'slider1', true);
echo get_post_meta($relative_page->ID, 'slider2', true);
echo get_post_meta($relative_page->ID, 'slider3', true);
?>
<?php endif; ?>


Philippe Dionne comments:

This look like the best solution but it doesn't work. The error log says get_post_by_slug is a call to an undefined function.


Daniele Raimondi comments:

Sorry, that was a custom function of mine :D
Add this function to your function.php file and the previous code should work:

/**
*
*
* Get page object using its slug
* @param unknown_type $slug
*/
function get_post_by_slug( $args=array() ) {
$defaults = array (
'posttype' => 'post',
'slug' => '',
);

// Parse incoming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $catid, $postnum.
extract( $args, EXTR_SKIP );
if ($posttype=='page')
$field='pagename';
else
$field='name';
$get_posts = new WP_Query;
$get_posts->parse_query(array($field => $slug, 'post_type' => $posttype, 'suppress_filters' => true));
$get_posts->init_query_flags();
$get_posts->is_singular = true;
$get_posts->is_page = true;
$get_posts=$get_posts->get_posts();
wp_reset_postdata();
if ( is_array($get_posts) && count($get_posts))
return $get_posts[0];
else
return false;
}


The code above works with both pages or posts (even with custom post types).


Daniele Raimondi comments:

Looking backward to the code (I wrote it some time ago) I think we can probably cut 2 or 3 lines of code. That should work also:

/**
*
* Get page object using its slug
* @param unknown_type $slug
*/
function get_post_by_slug( $args=array() ) {
$defaults = array (
'posttype' => 'post',
'slug' => '',
);

// Parse incoming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $catid, $postnum.
extract( $args, EXTR_SKIP );
if ($posttype=='page')
$field='pagename';
else
$field='name';
$get_posts = new WP_Query(array($field => $slug, 'post_type' => $posttype, 'suppress_filters' => true));
$get_posts=$get_posts->get_posts();
wp_reset_postdata();
if ( is_array($get_posts) && count($get_posts))
return $get_posts[0];
else
return false;
}

2011-08-13

John Cotton answers:

<blockquote>But this doesn't work:</blockquote>

It can't....on a category page post->ID isn't set to a value except in the loop.

Do you know the ID of the page you want the meta data for? If so just hard code it.

Or do you want those values output for each post displayed on the category page? If so, put that code in your loop, reference the global $post and then you can use $post->ID.

If neither of these are the case, you'll need to explain a little more what you want.

2011-08-13

Clifford P answers:

What do you mean by "does not work" - PHP error on the category page or sliders just don't show up or what?

Are you using a Slider plugin or do the sliders have some settings that affect where they can be displayed?

Are you using the exact same custom fields and image src for the page and category sliders?


Philippe Dionne comments:

1. PHP error

2. I'm not using any plugin for the slider.

3. Yes, the page and category templates have both the same custom field key.


Clifford P comments:

Copy the PHP error text please.

2011-08-13

Hai Bui answers:

Hi,

In the category template, you cannot get the post id, because it is not a post or a page. You have to find the id of those pages
- Publicité
- Corporatif
- etc..

and put it manually in the category template

<?php if ( is_category() ) :

// Show a slider on the category page
?>

<?php echo get_post_meta(enter_Publicité_page_id_here, 'slider1', true); ?>

<?php echo get_post_meta(enter_Corporatif_page_id_here, 'slider2', true); ?>

<?php echo get_post_meta(enter_another_page_id_here, 'slider3', true); ?>

<?php wp_reset_query(); ?>

<?php endif; ?>


It's the easiest way. There are ways to do it better but they would require more work.


Philippe Dionne comments:

This works but it's hardcoded. I'd like to find a solution without hard coding.

2011-08-13

Sébastien | French WordpressDesigner answers:

<?php if(is_category()) {
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<?php echo get_post_meta($post->ID, 'slider1', true); ?>
<?php echo get_post_meta($post->ID, 'slider2', true); ?>
<?php echo get_post_meta($post->ID, 'slider3', true); ?>

<?php endwhile; ?>
<?php else : ?>

<?php endif;
} ?>

2011-08-14

Marvin Shaker answers:


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (get_post_meta($post->ID, "slider1", true)) { ?>
<?php echo get_post_meta($post->ID, "slider1", true); ?>
<?php } ?>
<?php if (get_post_meta($post->ID, "slider2", true)) { ?>
<?php echo get_post_meta($post->ID, "slider2", true); ?>
<?php } ?>
<?php if (get_post_meta($post->ID, "slider3", true)) { ?>
<?php echo get_post_meta($post->ID, "slider3", true); ?>
<?php } ?>
<?php endwhile; endif; ?>


or


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php echo get_post_meta($post->ID, "slider1", true); ?>
<?php echo get_post_meta($post->ID, "slider2", true); ?>
<?php echo get_post_meta($post->ID, "slider3", true); ?>
<?php endwhile; endif; ?>