Forum Replies Created

Viewing 15 replies - 271 through 285 (of 318 total)
  • Joey

    (@leglesslizard)

    OK so a typical query to grab the posts would look something like this:

    $args = array(
    	'post_type'           => 'post',
    	'post_status'        => 'draft',
    	'posts_per_page' => -1
    );
    $draft_posts = get_posts( $args );

    This would give you an array of post objects containing each and every post where the post status is “draft”.

    Using this you could run a foreach loop to update each one’s status to “publish”:

    foreach( $draft_posts as $post ) {
            $updated_post = array(
                    'ID'                => $post->ID,
                    'post_status' => 'publish',
            );
            wp_update_post( $updated_post );
    }

    The method itself should be as simple as that but it’s the cron that takes a bit more work. The only way I’ve done this is by registering an activation hook that sets up the new cron job (when plugin is activated; cron job is registered). There are some great step by steps for this out there including this one which should give you everything you need. If you’re unsure how to go about creating a plugin again there are a tonne of tutorials out there but the codex is a great place to start ??

    Edit: Just found this function which may be a neater alternative if all you want to do is adjust the post status – wp_publish_post()

    Joey

    (@leglesslizard)

    Always think “is there a wordpress function for that?” ?? Don’t try and do stuff that’s already done for you! See here.

    There are so many options with this function I find it hard to believe you couldn’t get exactly what you wanted with it. Pay particular attention to the “Only Show Children of a Category” example as I believe that is what you are describing above.

    Joey

    (@leglesslizard)

    Well, hopefully someone with a bit more experience than me will chime in and give you a simple solution.

    My response was due to work I’ve done on shop sites where, for example, we have a post type of products and then various categories:

    - Books
        - Authors
            - Author 1
            - Author 2
        - Genre
            - Genre 1
            - Genre 2
    - Music
        - Artists
            - Artist 1
            - Artist 2
        - Genre
            - Genre 1
            - Genre 2
    etc etc

    This is all managed by categories and then within the templates you have control over what to display. An archive page for music (for example) could provide a dropdown filter with artists,genre etc and the sub categories could be shown dependant on the selection.

    I know it can all be managed this way but whether this is the best way to go about it I’m not 100% sure

    Joey

    (@leglesslizard)

    Glad you got it working ??

    I would just make one minor adjustment. Currently your steps are:
    – Set the output, term and url variables
    – Check term variable and carry out code depending on it’s value
    – Set another url variable and another output variable
    – Return the newest output variable

    I would shorten this down to the following:
    – Set the term variable
    – Check term variable and carry out code depending on it’s value
    – Return output

    Currently you are creating a link with a url that is never going to be used ($url) as this is either not returned (default) or replaced by another url. Therefore, there is no real need to set it in the first place. This also avoids the need to run a string replace.
    Returning the values when they are found allows your application to carry on immediately. No big deal assigning the return value to a variable but if it’s just going to be returned anyway, why the extra step? ??

    So, my cases would look like:

    case 'foo':
        return '<a href="value_of_new_url_here">Link text</a>';

    I wouldn’t bother with the $output, $url, $newoutput or $newurl variables at all ??
    There’s nothing wrong with what you have there (and I’m definitely not an expert!) but I try and minimise to only what I need where possible as long as I don’t compromise readability.

    Regards,
    Joey

    P.S. I hope what I’ve written makes sense but as an extra “but why?” type statement…variables you’ll use here there and everywhere and they’re great. But they are there to remove the need to hardcode values and avoid repetition. In your example your variables are only used once and you are hardcoding (writing the values rather than pulling them in from somewhere else, think static vs dynamic) anyway ?? so you don’t always have to create variables.

    Joey

    (@leglesslizard)

    Hey,

    Sounds quite complex but maybe this is what you’re after?

    Personally, I’d be looking at custom taxonomies rather than handling it all through the post types I think. Maybe create a custom post type for episodes and a series->series-season-number type structure for taxonomies. I am a lot more familiar with taxonomies than hierarchical post types however :p but this way each episode would be tagged with its parent season and grandparent? series.

    Good luck!
    Joey

    Joey

    (@leglesslizard)

    Hey,

    Everything prefixed with the “$” symbol is a variable in PHP. All this does is holds a value (this can be a string, integer, array, object etc.). So whenever you want access to these values you have to reference the variable exactly. For example, I can set $term to equal 1 by writing:
    $term = 1;

    Now every time I want to use 1 within my code I can reference $term:

    echo $term + 1; // 2
    echo "I have " . $term . " bunny."; // I have 1 bunny.

    The extract function in PHP takes an array and creates variables using the keys and assigns those variables the corresponding value. So what you want to do in your code is check which attribute has been assigned ($term?) and then just return the desired string e.g.

    case 'foo':
        return '<a href="https://foourl.com">Link Text</a>';

    If you are hardcoding the urls like above then this approach would work but as has been said, getting a grasp on variables will help you a tonne when working on little functions like this ?? A good way to see what is happening in your code is to use breakpoints and analyse what variable have been assigned at what points in your code. If you don’t know how to use breakpoints then try this, if I wanted to see what was going to be returned in your code example I would do:

    case 'foo':
        $url = 'https://foourl.com';
        var_dump($output);
        die();
        return $output;

    This will dump out on the screen the value of $output at that moment and kill the application. This gives you a nice idea of what’s going on and you can dump out multiple values as a comma separated list using var_dump() BUT do remember to remove it (along with the die()!) if you want your application to work :p

    A nice simple explanation of what the shortcode_atts() function does can be found here too.

    Hope that helps some,
    Joey

    Joey

    (@leglesslizard)

    Hey,

    WordPress has a nifty little way to setup cron jobs. See this page of the codex for a nice example of setting up an hourly function on plugin activation.

    To change a post’s status you’d be looking at wp_update_post. What you’d need to do is:
    – return all posts (setup a query to return only those you need) see get_posts
    – run a foreach loop to update each post

    Unless this applies to EVERY post I’d advise adding a distinguishing feature to the posts in order to return only those ones in the query (maybe a category “auto publish” for example).

    That should get you on your way though! Hope it helps,
    Joey

    Forum: Hacks
    In reply to: Hover Effects
    Joey

    (@leglesslizard)

    Hi,

    I might be wrong (quite possibly!) but believe the only way to achieve this would be to use javascript and I’d advise looking at jQuery if this isn’t an avenue you want to venture down as it is a lot simpler ??

    You’d need to enqueue a javascript file within your child theme on the pages that you need to add this effect (information for enqueuing javascript and child theme information is all well documented in the codex). Within the javascript file (.js) you’d define exactly what you wanted to do within a jQuery block. So, for example, I’d look at the IDs of the elements I wanted and when the user hovered over element1, I’d add an effect to element2:

    jQuery( document ).ready( function( $ ){
        $( '#element1' ).hover( growImage, returnImageToOriginal );
    
        function growImage() {
            $( '#element2' ).css( {height: '+=10%', width: '+=10%'} );
        }
    
        function returnImageToOriginal() {
            $( '#element2' ).css( {height: "", width: ""} );
        }
    });

    I’m not too hot on javascript but this should put you on the right path. What this code would do is look at the first element (it’s ID here is set to element1, # denotes ID) and when the user is over it invoke function “growImage”. This is defined below and this will change the style of the second element (ID element2) to make it 10% bigger. The second function will be called when the user ceases to hover over the first element anymore and this will remove the style properties that we just added.

    Hope this helps some! Seriously, loads on info out there if you look around ??
    Joey

    Joey

    (@leglesslizard)

    Anything is possible ;p

    I would think the best way for you to achieve this would be CSS positioning and use of the z-index property. There is a quick write up here showing examples that sound similar to what you want but it is widely covered across the net.

    Hope that helps ??
    Joey

    Forum: Hacks
    In reply to: Widget variant issue
    Joey

    (@leglesslizard)

    Hey,

    Sorry if I misunderstand but to place the widget into 2 different widget areas aren’t you going to be “cloning” the widget regardless?

    Two (possibly) quick solutions come to mind:
    I obviously don’t know your exact use case but I would look at either finding/coding a separate widget so that each area has it’s own widget behaving how I want them to
    OR
    Hide/show the elements of the widgets using css/js to output the desired effect.

    I would have a look at the plugin itself, however, as many people supply you with hooks and filters to adjust the output of such things before it is returned. The author may have implemented something that enables you to do what you want.

    Hope that helps some!
    Joey

    Joey

    (@leglesslizard)

    Hey mate,

    Simply change the post type of “post”:
    `// Return all existing posts
    $posts = get_posts( array( ‘post_type’ => ‘post’ ) );`

    To “page”:

    // Return all existing posts
    $posts = get_posts( array( 'post_type' => 'page' ) );

    In order to activate this just drop the file with the code into your plugins folder (wp-content/plugins) and go to wordpress admin->plugins and activate.

    Be warned, this code does the same as the last code and will delete pages that have the same title so use with caution ??

    Joey

    Forum: Hacks
    In reply to: Categories Title Change
    Joey

    (@leglesslizard)

    Hey,

    The code above is what is displayed when you click on a date (for example the archives widget shows links to certain date archives). For displaying categories when clicking on different category names you want to be looking in the category.php template:

    <header class="archive-header">
      <h1 class="archive-title"><?php printf( __( 'Category Archives: %s', 'twentyfourteen' ), single_cat_title( '', false ) ); ?></h1>
    
        <?php
          // Show an optional term description.
          $term_description = term_description();
          if ( ! empty( $term_description ) ) :
            printf( '<div class="taxonomy-description">%s</div>', $term_description );
          endif;
        ?>
    </header><!-- .archive-header -->

    This should show a title with “Category Archives: Your Category Title” in the header.

    Hope that helps a tad!

    Joey

    (@leglesslizard)

    Hey,

    You could limit the scope for the current file by wrapping it in a function. See examples here.

    Hope this helps!

    Joey

    (@leglesslizard)

    Hey,

    This line of code here
    $j('.side_menu_button_wrapper a.side_menu_button_link, a.close_side_menu').click(function(e){
    is looking at the selectors “side_menu_button_wrapper a.side_menu_button_link” and “a.close_side_menu” and carrying out the functionality below it if they are clicked.

    Can you just add your class into that list so that it does the same thing? Like so:
    $j('.side_menu_button_wrapper a.side_menu_button_link, a.close_side_menu, .side-open-button').click(function(e){

    Bear in mind its not a great idea to modify theme files directly and you should look into creating a child theme to handle any custom modifications you make.

    Hope that helps! Good luck!

    Joey

    (@leglesslizard)

    Hey,

    I copied and pasted your snippet above and it works fine. What it’s doing is grabbing the content of the given url which is a string of data in json format. Then you use json_decode to format the data into a php object so that it is easy to work with and if the successful parameter is set, echo out the “rate” within the string.

    This should work anywhere as long as it is inside php tags “<?php code goes here ?>” but if you wanted to check what is being returned from the url try sticking var_dump( $result ); before the if statement and seeing what is actually being retrieved.

    Hope this helps! Good luck!

Viewing 15 replies - 271 through 285 (of 318 total)