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

Post - link to the child not the parent WordPress

  • SOLVED

I have this page set up and looking like I want it to but the problem is the query calls the category and displays them but it then links to itself and not the products in it's category.

Example - it is currently doing this
Junior Play . http://newplayscape.wkwd.com.au/title-is-here-3/

I would like it to do this:
Junior Play > http://newplayscape.wkwd.com.au/category/2-5-years/


The query I am using on the is:

<ul class="playscapes-products">
<?php
query_posts('showposts=-1&cat=20&orderby=date&order=ASC');
while (have_posts()) : the_post();
?>
<li id="post-<?php the_ID(); ?>" class="clearfix">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

<div id="Product-Image">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('medium');
}
?>
</div>
<h3><?php the_title(); ?></h3>
<div id="Content-For-Product"><?php the_content(25);?> </div>
</a>
</li>
<?php
endwhile;
?>
</ul>


Is there a way to change it to add this link.

The link could be stand alone not wrapping the item.

Answers (4)

2015-04-09

timDesain Nanang answers:

please try these steps:
1. install and activate plugin: https://wordpress.org/plugins/categories-images/
2. assign the thumbnail for each category (http://domain/wp-admin/edit-tags.php?taxonomy=category)
3. put the following code in the category-products.php or category-20.php (outside the loop)


<?php
while (have_posts()) : the_post();
//no need the loop
endwhile;
?>

<ul class="playscapes-products">
<?php
$categories = array( 31, 3, 32, 33, 35, 34 );
foreach( $categories as $cat_id ){
?>
<li>
<a href="<?php echo get_category_link($cat_id); ?>" title="<?php echo get_cat_name($cat_id);?>">
<div id="Product-Image">
<?php if( function_exists('z_taxonomy_image') AND get_option('z_taxonomy_image'.$cat_id)<>'' ) z_taxonomy_image($cat_id); ?>
</div>
<h3><?php echo get_cat_name($cat_id);?></h3>
<p class=""><?php echo category_description($cat_id);?></p>
</a>
</li>
<?php
}
?>
</ul>


Amanda comments:

thank you @ timDesain Nanang.
this solution worked perfectly.

2015-04-09

Arnav Joy answers:

can you describe more about your problem?


Arnav Joy comments:

you can also try


<?php $category_link = get_category_link( 20 ); ?>
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>

read more here

https://codex.wordpress.org/Function_Reference/get_category_link


Amanda comments:

I need this page -http://newplayscape.wkwd.com.au/category/products/

to show the category "products" (which it is)

But when I click on 5-12 Years it takes me to http://newplayscape.wkwd.com.au/title-7/ and I would really like it to take me to http://newplayscape.wkwd.com.au/category/5-12-years/


Arnav Joy comments:

please try this


<?php

$cat_slugs = array('2-5-years','5-12-years','all-abilitiessensory-pla','outdoor-fitness','skatewave','aquatic-playscape');

?>

<ul class="playscapes-products">

<?php

$i=0
query_posts('showposts=-1&cat=20&orderby=date&order=ASC');

while (have_posts()) : the_post();

?>

<li id="post-<?php the_ID(); ?>" class="clearfix">

<?php /*?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php */?>
<a href="<?php echo site_url().'/category/'.$cat_slugs[$i].'/'; ?>" title="<?php the_title(); ?>">

<div id="Product-Image">

<?php

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.

the_post_thumbnail('medium');

}

?>

</div>

<h3><?php the_title(); ?></h3>

<div id="Content-For-Product"><?php the_content(25);?> </div>

</a>

</li>

<?php

$i++; endwhile;

?>

</ul>

2015-04-09

Jayaram Y answers:

Hi,

In your permalinks select the category base as : /%category%/%postname%


Amanda comments:

it says there is an error on this line

query_posts('showposts=-1&cat=20&orderby=date&order=ASC');

2015-04-09

Andrea P answers:

showposts has been deprecated since ages.

use
posts_per_page=-1

instead of
showposts=-1

then your issues seems coming from the fact that you are not listing the real categories there, but rather you are listing posts which you have named with the name of the categories and all those posts are under the category id=20. is this the case?

if it is, you will need something which put in relation those posts with the actual categories archives.
Arnav's solution could work (by changing the showposts) but it won't be future proof. if you add a category (aka post-placeholder-for-category) or you change the title of either a category or one of those category-posts, that code won't work anymore.

the easiest way would be to keep your structure and just add a custom field to those posts, where you can set the category where you want them to link. but maybe it would be better to plan and implement a complete different structure..



***** UPDATED *****

you could try installing this plugin:
https://wordpress.org/plugins/category-thumbnails/

it is not been updated since 1 year, but it seems still working (though I haven't found any new plugin to do the same at the moment..).

after installing, you'll be able to add a thumbnail image to your post categories. you then add the description as well, which is a default feature. and then you have all you need to loop your categories and display a grid like the one you have in the products page.

you could try with a code like that:
(note that you first need to group all the products main categories under a parent category, so we can loop only the categories that you actually need in that page)


<ul class="playscapes-products">

<?php
// ATTENTION! create a category called something like product-kind, place all your porducts main categories as a child of that, and put its ID in the function below
$categories = get_categories('child_of=20');

foreach ($categories as $category){

?>

<li id="post-<?php echo $category->slug; ?>" class="clearfix">

<a href="<?php echo get_category_link( $category->term_id ); ?>" title="<?php echo $category->name; ?>">



<div id="Product-Image">

<?php

if ( has_category_thumbnail($category->term_id) ) { // check if the category has a Post Thumbnail assigned to it.

the_category_thumbnail($category->term_id);

}

?>

</div>

<h3><?php echo $category->name; ?></h3>

<div id="Content-For-Product"><?php echo $category->description; ?> </div>

</a>

</li>

<?php

}

?>

</ul>