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

Need help creating a link in my homepage (php help) WordPress

  • SOLVED

Hi,

So my homage has various categories with each category having various articles (posts) bellow them.
The title of the category in the page's body are plain text and i would like them to become a link to the respective category. I want to maintain the site's functionality, so this has to be a php work ( i know one can do it via css, but thats not what im looking)

The categories are: Ciencia & Salud, Cultura & Gastronomía,Decoración,Finanzas & Economía....

I will grant you access to the server for you to do what you need to do.

I believe the file that needs to be modified is post-layout-large-small-1-col.php located in wp-content>themes>Neue>templates>postbox>. This file affects all the categories, so its only one line of code that needs to be created.

The site url:
104.236.255.212

user:root
Password:polloloco

Answers (5)

2015-03-24

Andrea P answers:

Hello dipka,

I think that one is not the file which should be edited.

you should start looking at the homepage template, and then look at which part is included where it comes to display the categories.

try editing your homepage and look on the rightside if there is a specific template in use.
if yes, search for a file called similarly, which should have that very name written on top of the file content.
if is not using a specific template, it could be using a file called home.php or front-page.php

then browse that code till you find the div which wraps the categories boxes, and look at which template part is included using the function get_template_part().

it should be a very easy task as soons as you've found the correct template file.
the login is expired, so if you want to send a new one to me on a private message, I'll have a look and post the precise directions

cheers


****UPDATED****

if that is the template part which displays one of the categories boxes, you should post what there is before it, because I see that the title at this point is stored into a variable $title, which is not a global.
at the point where it sets that variable, there should be a category query, or something similar, and then it stores the title and loop each category printing a box with the latest posts for each one.

if you post that bit of code, I can tell you how to retrieve the category link along with the title, and store it in another variable that then you can use to create an html a-href markup around the title. but I need to see how the categories data is retrieved and looped, otherwise I can't tell you how you'd do it. there are several ways to get a category title+link, but it depends on how is made your loop.


*** UPDATE 2 ****
looking at your screenshot and at the last piece of code you've posted, I'd say that the variable $title is the category name with applied
a shortcode which makes the last word to be blue. in fact the variable $title is printed using the do_shortcode() function.

it would be better to see the parent template file of the file you've posted (the one in which that template part you've posted, is included using get_template_part ), because in that file I would retrieve the category link in a more elegant way.
but I think this would work for you as well:



<?php $category_url = home_url().'/'.get_option( 'category_base' ).'/'.sanitize_title( strip_shortcodes($title) ); ?>

<div class="col-sm-12">

<h2 class="vw-post-box-layout-title"><a href="<?php echo $category_url; ?>" target="_blank" title="<?php echo strip_shortcodes($title); ?>"><?php echo do_shortcode( $title ); ?></a></h2>

</div>




this code is assuming that you have a base set for categories archives (like: your_home.com/category/category-page ). if you don't have it and your archives slugs are directly after the root url, you should take off the part
get_option('category_base').'/'.


dipka comments:

Andrea i got it working like this
<div class="vw-post-box-layout vw-post-box-layout-large-small-1-col">
<?php if ( isset( $title ) && ! empty( $title ) ) : ?>
<div class="vw-post-box-layout-title-wrapper">
<div class="row">
<?php $category_url = home_url().'/'.get_option( 'category_base' ).'/'.sanitize_title( strip_shortcodes($title) ); ?>
<div class="col-sm-12">
<h2 class="vw-post-box-layout-title"><a href="<?php echo $category_url; ?>" <?php echo do_shortcode( $title ); ?></a></h2>
</div>
</div>
</div>
<?php endif; ?>

<div class="row">
<div class="col-sm-12">
<?php if( have_posts() ) : the_post(); ?>
<?php get_template_part( 'templates/post-box/post-box-large', get_post_format() ); ?>
<?php endif; ?>

<div class="block-grid block-grid-xs-1">

<?php while( have_posts() ) : the_post(); ?>
<div class="block-grid-item">
<?php get_template_part( 'templates/post-box/post-box-small', get_post_format() ); ?>
</div>
<?php endwhile; ?>

</div>
</div>
</div>
</div>


However its not getting the category slug. Instead its getting the text like this (http://localhost:8888/Seccion/ciencia-salud) while the actual slug/link is (http://localhost:8888/Seccion/ciencia-y-salud).

I know this is minor cause i can just change the slugs to match that text, however to maintain functionality it would be cleaner if it got the slugs name.


Andrea P comments:

ah ok, I haven't considered that you might sometimes have the slug which is different from the name.

this other code should work then:


<div class="row">

<?php
$cat_obj = get_term_by( 'name', strip_shortcodes($title), 'category' );
$category_url = get_category_link( $cat_obj->term_id );
?>

<div class="col-sm-12">

<h2 class="vw-post-box-layout-title"><a href="<?php echo esc_url( $category_url ); ?>" > <?php echo do_shortcode( $title ); ?> </a></h2>

</div>

</div>



keep in mind that this is not the most elegant way to do it, because to do it properly, I will have to edit the parent template file in which this template part is included. in that file, there is surely another function which gets the category object, and saves the category name as the variable $title by adding the shortcode. the most economic solution will be to simply use the same category object, and store a variable with the permalink as well, so that we can use it in this template part.

also, please be aware that in the code you've posted you've missed a > to close the a href before printing the title. if you paste my code and overwrite yours, it will be fine.

2015-03-25

timDesain Nanang answers:

Hi Sir,
Try to replace this part:
<h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2>
with:
<h2 class="vw-post-box-layout-title"><?php echo '<a href="'.get_category_link($category).'">'.do_shortcode( $title ).'</a>';?></h2>

2015-03-24

Navjot Singh answers:

Please don't give out the site login out in the open. Give it to only the person who would be doing the job.


Navjot Singh comments:

Also can you paste the file's code here?


dipka comments:

thanks for the advise, but this is a droplet that was created for the purpose of this question, it will deleted as soon as there is a question.


dipka comments:

<div class="vw-post-box-layout vw-post-box-layout-large-small-1-col">
<?php if ( isset( $title ) && ! empty( $title ) ) : ?>
<div class="vw-post-box-layout-title-wrapper">
<div class="row">
<div class="col-sm-12">
<a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php the_title(); ?><h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2></a>
</div>
</div>
</div>
<?php endif; ?>

<div class="row">
<div class="col-sm-12">
<?php if( have_posts() ) : the_post(); ?>
<?php get_template_part( 'templates/post-box/post-box-large', get_post_format() ); ?>
<?php endif; ?>

<div class="block-grid block-grid-xs-1">

<?php while( have_posts() ) : the_post(); ?>
<div class="block-grid-item">
<?php get_template_part( 'templates/post-box/post-box-small', get_post_format() ); ?>
</div>
<?php endwhile; ?>

</div>
</div>
</div>
</div>


Navjot Singh comments:

Are you talking of the post boxes? Because in that title is a custom one which you select and the categories of the respective posts inside box are already linked.

2015-03-24

nat_c answers:

The code that needs to be edited is inside the two files your codes calls (the template parts).

This is the code we need to see in order to tell you what to change to display the categories as links and not as text.

However, the most common 'issue/solution pair' would be to fetch the categories using the the_category(); function instead of something else (like <em>get_the_category_list</em>), to "auto-apply" the links.

2015-03-24

Arnav Joy answers:

in this block , where you have displayed category name ?

<div class="vw-post-box-layout vw-post-box-layout-large-small-1-col">

<?php if ( isset( $title ) && ! empty( $title ) ) : ?>

<div class="vw-post-box-layout-title-wrapper">

<div class="row">

<div class="col-sm-12">

<a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php the_title(); ?><h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2></a>

</div>

</div>

</div>

<?php endif; ?>



<div class="row">

<div class="col-sm-12">

<?php if( have_posts() ) : the_post(); ?>

<?php get_template_part( 'templates/post-box/post-box-large', get_post_format() ); ?>

<?php endif; ?>



<div class="block-grid block-grid-xs-1">



<?php while( have_posts() ) : the_post(); ?>

<div class="block-grid-item">

<?php get_template_part( 'templates/post-box/post-box-small', get_post_format() ); ?>

</div>

<?php endwhile; ?>



</div>

</div>

</div>

</div>


dipka comments:

Sorry this is the right code. I was playing around with it and forgot to errase some left over code.
<div class="vw-post-box-layout vw-post-box-layout-large-small-1-col">
<?php if ( isset( $title ) && ! empty( $title ) ) : ?>
<div class="vw-post-box-layout-title-wrapper">
<div class="row">
<div class="col-sm-12">
<h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2></a>
</div>
</div>
</div>
<?php endif; ?>

<div class="row">
<div class="col-sm-12">
<?php if( have_posts() ) : the_post(); ?>
<?php get_template_part( 'templates/post-box/post-box-large', get_post_format() ); ?>
<?php endif; ?>

<div class="block-grid block-grid-xs-1">

<?php while( have_posts() ) : the_post(); ?>
<div class="block-grid-item">
<?php get_template_part( 'templates/post-box/post-box-small', get_post_format() ); ?>
</div>
<?php endwhile; ?>

</div>
</div>
</div>
</div>


Arnav Joy comments:

still I don;t find the line where category name is displayed.


dipka comments:

Here is a screen shot of how the <div class="col-sm-12”> is displayed.
[[LINK href="https://www.dropbox.com/s/uc44m5j8cegntkq/Screenshot%202015-03-24%2013.30.35.png?dl=0"]][[/LINK]]

The line that display the category title is the following:
<h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2></a>

I was thinking that i could do something like this:
<code><a href=“<?PHP function to get the category slug or link?>”><h2 class="vw-post-box-layout-title"><?php echo do_shortcode( $title ); ?></h2></a>


dipka comments:

https://www.dropbox.com/s/uc44m5j8cegntkq/Screenshot%202015-03-24%2013.30.35.png?dl=0
[[LINK href="https://www.dropbox.com/s/uc44m5j8cegntkq/Screenshot%202015-03-24%2013.30.35.png?dl=0"]][[/LINK]]


Arnav Joy comments:

you can use following to get category

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


dipka comments:

Sorry maybe i did not explained my self. I do not know PHP so its a bit hard for me to try and guess hats the correct function to use.

I have tried all the get options on the link and i can’t get anything in return besides int(0).