WordPress.org

Forums

[resolved] I trying to modify a code.. help please (7 posts)

  1. DeveWeb
    Member
    Posted 2 years ago #

    hi team, well i have a problem, I not have much experience in php and I need change a code, i need change this code

    1)

    $postheader .= '<span class="entry-meta"><span class="entry-comments">' . get_comments_number() . '</span>';

    for this code

    2)

    <span class="entry-meta"><span class="entry-subcat">
    <?php
    $cats = get_the_category();
    if (count($cats) > 1) {
    echo $cats[1]->cat_name;
    } else {
    echo $cats[0]->cat_name;
    }
    ?>

    I tried to change it but I get an error, i tried to change it for this code

    3)

    $postheader .= '<span class="entry-meta"><span class="entry-subcat">' . $cats = get_the_category(); if (count($cats) > 1) { echo $cats[1]->cat_name; } else { echo $cats[0]->cat_name; };

    can someone help me please.!!

  2. Andrew Bartel
    Member
    Posted 2 years ago #

    What are you trying to do? Can you provide a link to your site?

  3. DeveWeb
    Member
    Posted 2 years ago #

    hi thanks for you reply, I want the code number 2) modify it as number 1) I tried to do it and the result is the code number 3) I dont have active the website, I'm designing and programming on localhost :(

  4. Andrew Bartel
    Member
    Posted 2 years ago #

    What do you mean modify it as a number? Cast the variable as an integer?

    It looks like you're checking the number of categories assigned to a post and if there's more than one, display the second result, otherwise display the first result.

    Assuming you're within the loops, this will work:

    $cats = get_the_category(get_the_ID());
    
    if(count($cats) > 1) echo $cats[1]->cat_name;
    else echo $cats[0]->cat_name;

    If you're not, sub get_the_ID() for a $id of the post you want to check.

  5. DeveWeb
    Member
    Posted 2 years ago #

    no dude does not work, let me explain better, sorry my English is little bad, I just want to put this code

    <?php
    $cats = get_the_category();
    if (count($cats) > 1) {
    echo $cats[1]->cat_name;
    } else {
    echo $cats[0]->cat_name;
    }
    ?>

    in the middle of this code

    $postheader .= '<span class="entry-meta"><span class="entry-subcat">' . #####HERE###### . '</span>';

    look the code number 1)i wanna remove

    get_comments_number()
    and put it the code number 2) thanks dude

  6. Andrew Bartel
    Member
    Posted 2 years ago #

    <?php
    $cats = get_the_category(get_the_ID());
    
    if(count($cats) > 1) $pageheader = $cats[1]->cat_name;
    else $pageheader = $cats[0]->cat_name;
    
    $postheader .= '<span class="entry-meta"><span class="entry-subcat">'. $pageheader.'</span>';
    
    ?>
  7. DeveWeb
    Member
    Posted 2 years ago #

    dudeeeee you areee the best, work perfect... really thanks a lotttt :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.