0

Hopfully I can explain this, I'm confusing myself now.

I have Template called 'Landing' that is the template for a number of different pages in Wordpress.

Each of the pages using this template will contain different blocks of information.

These blocks contain a title, an image, text and a link, I'm calling them Content_Widgets, they are not Widgets in the Wordpress sense.

My Content_Widgets are each posts and I have created a custom post type called Content_Widget.

Now I need to load the correct posts on the correct page.

I thought I could do this by having a category on the page in Wordpress and then the same category on the post for that page.

So it would be like load the post that have a category the same as this page.

Is this a good way to load the correct post on the correct page?

So this is the code I'm using but it's not outputting anything.(I'm using Advanced Custom Fields in the Custom post type.)

The correct category name outputs in the h1 but nothing else.

Anyone have any ideas why this isn't working, or is this the wrong way to do this??

    <div id="contentWidgets">

            <?php
                                    // get category name for page
                $cat = get_the_category($post->ID);
                $catName = $cat[0]->name;
                echo $catName;

                $content_args = array(
                    'post_type'         => 'Content_Widget',
                    'category_name' => $catName                 
                );

                $content_loop = new WP_Query($content_args);

                if($content_loop->have_posts()):
                    while($content_loop->have_posts()):
                        $content_loop->the_post();

                $linkImg = get_field('image');
                $txt = get_field('text');
                $link = get_field('link');

            ?>

            <h1><?php echo $catName;?></h1>

            <dl>
                <dt><?php the_title(); ?></dt>
                <dd><img src="<?php echo $linkImg; ?>" alt=""></dd>
                <dd><?php echo $txt; ?></dd>
                <dd><a href="<?php $link; ?>">Find out more</a></dd>
            </dl>


            <?php

                endwhile;
                endif;

                wp_reset_postdata();

            ?>

        </div><!--contentWidgets-->
3
  • you use both $catName and $cat_Name, is that a typo here?
    – Milo
    Aug 29, 2013 at 16:39
  • also see register_post_type: $post_type (max. 20 characters, can not contain capital letters or spaces)
    – Milo
    Aug 29, 2013 at 16:41
  • $catName was a typo just in this example, I've updated. I've changed the Custom Post Type name to all lowercase but I'm still not getting any results. Aug 29, 2013 at 16:54

1 Answer 1

1

category_name is a bit misleading, it expects the slug, not the name. Try:

$content_args = array(
    'post_type' => 'Content_Widget',
    'category_name' => $cat[0]->slug          
);
3
  • So thought I had this but I have another slight problem. Because the page that the post are loading into has the same category that is in the output. Is it possible to skip the first entry in the loop. Aug 29, 2013 at 18:30
  • not sure I understand what you're saying. you can skip querying the first item by adding 'offset' => 1 to the query, does that do what you need?
    – Milo
    Aug 29, 2013 at 20:23
  • 'offset' => 1 fixed it. Aug 30, 2013 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.