How do I get the slug of a field into my template?

This topic is: not resolved

This topic contains 6 replies, has 3 voices, and was last updated by  laura 3 years, 10 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #158595

    sc0ttkclark
    Keymaster
    Post count: 791

    You should really be fine with this, just edit your "Name" column and change it’s label to "Anchor ID". Other than that, you’re still going to need to enter a value into the name column each time you save.

    You could rename the label for "name" to be "Last Name", and then add a separate field for "anchor_id" which could then be left empty and auto-generated using an "After" helper from the name and first_name columns..

    #158596

    laura
    Member

    I can’t change the name label or change it to last name as these fields are used for other purposes and my client will be filling in the data and it needs to be clear. I just need a way to convert the name to have no spaces, but I am not sure where to do that. I thought I could make a helper to do so, but I am not sure how that works. I tried this in a helper, but I don’t think I have it quite right and am not sure how to get the output into my template.
    <pre>
    <?php
    $essay_author_slug = $this->get_field(‘essay_author’);
    $essay_author_slug = str_replace(‘_’, ‘ ‘, $essay_author_slug);
    $output = ‘$essay_author_slug’;
    ?>
    </pre>

    #158597

    logikal16
    Member

    @laura – Which are you trying to create the slug from?

    A. the "name" column
    B. the "first_name" and "last_name" columns together
    C. some other text column

    (A) Create a column named "slug" (column type: slug). When you go to add or edit an item, leave the slug field blank, and it’ll auto-generate a slug using the "name" column.

    <pre><h3 class="name" id="{@slug}">{@last_name}, {@first_name}</h3></pre>

    (B)

    Display helper: "person_slug"
    <pre><?php
    $first_name = $this->get_field(‘first_name’);
    $last_name = $this->get_field(‘last_name’);
    echo sanitize_title($first_name . ‘ ‘ . $last_name);
    ?></pre>

    Template:
    <pre><h3 class="name" id="{@name, person_slug}">{@last_name}, {@first_name}</h3></pre>

    (C)

    Display helper: "column_slug"
    <pre><?php echo sanitize_title($this->get_field($name)); ?></pre>

    Template:
    <pre><h3 class="name" id="{@essay_author, column_slug}">{@last_name}, {@first_name}</h3></pre>

    #158598

    laura
    Member

    That was really helpful, thank you! Is it possible to do this with a PICK column? If not, can I create a text column that pulls the selected value of a PICK column automatically?

    #158599

    logikal16
    Member

    Yeah, that’s possible too. Here’s a modified version of the above "column_slug" helper:

    <pre><?php
    // The PICK column is an array
    // $name and $value are automatically created
    // $name is the column name passed in from the Template
    // $value is the column’s value
    $pick_columns = $this->get_field($name);

    // We’re assuming the pod referenced from the PICK column has "first_name" and "last_name" columns
    $first_name = $pick_columns[0]['first_name'];
    $last_name = $pick_columns[0]['last_name'];

    // Slug-ify the name
    echo sanitize_title($first_name . ‘ ‘ . $last_name);
    ?></pre>

    It might look confusing, but play around with the code for a little while and you’ll see what’s going on.

    #158594

    laura
    Member

    I have this in my template:
    <pre>
    <h3 class="name" id="{@name}">{@last_name}, {@first_name}</h3>
    </pre>

    I would like to have the slug of the name field in my id instead of the title, so that I can use it as an anchor. Is there something I am supposed to use to substitute for @name or is it something more complicated?

    Thanks!

    #158600

    laura
    Member

    I am relatively new to WordPress and PHP, so your thorough commenting is extremely helpful. This worked perfectly and I pretty much understand what is going on. Thanks!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Wordpress Cloud Hosting